blob: 5bea0ab5cd7c0bea51cfd6aa1edd92a7784cf5d4 [file] [log] [blame]
Narayan Kamath9823e782016-08-03 12:46:58 +01001/*
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_METHOD_HANDLES_H_
18#define ART_RUNTIME_METHOD_HANDLES_H_
19
20#include <ostream>
21
Narayan Kamath208f8572016-08-03 12:46:58 +010022#include "dex_instruction.h"
Orion Hodsonba28f9f2016-10-26 10:56:25 +010023#include "handle.h"
Narayan Kamath208f8572016-08-03 12:46:58 +010024#include "jvalue.h"
Orion Hodsonba28f9f2016-10-26 10:56:25 +010025#include "mirror/class.h"
Narayan Kamath208f8572016-08-03 12:46:58 +010026
Narayan Kamath9823e782016-08-03 12:46:58 +010027namespace art {
28
Narayan Kamath208f8572016-08-03 12:46:58 +010029namespace mirror {
Orion Hodsonc069a302017-01-18 09:23:12 +000030 class MethodHandle;
Narayan Kamath208f8572016-08-03 12:46:58 +010031 class MethodType;
Orion Hodson811bd5f2016-12-07 11:35:37 +000032} // mirror
Narayan Kamath208f8572016-08-03 12:46:58 +010033
34class ShadowFrame;
35
Orion Hodson1a06f9f2016-11-09 08:32:42 +000036// Returns true if there is a possible conversion from |from| to |to|
37// for a MethodHandle parameter.
38bool IsParameterTypeConvertible(ObjPtr<mirror::Class> from,
39 ObjPtr<mirror::Class> to);
40
41// Returns true if there is a possible conversion from |from| to |to|
42// for the return type of a MethodHandle.
43bool IsReturnTypeConvertible(ObjPtr<mirror::Class> from,
44 ObjPtr<mirror::Class> to);
45
Orion Hodsonba28f9f2016-10-26 10:56:25 +010046// Performs a conversion from type |from| to a distinct type |to| as
47// part of conversion of |caller_type| to |callee_type|. The value to
48// be converted is in |value|. Returns true on success and updates
49// |value| with the converted value, false otherwise.
50bool ConvertJValueCommon(Handle<mirror::MethodType> callsite_type,
51 Handle<mirror::MethodType> callee_type,
52 ObjPtr<mirror::Class> from,
53 ObjPtr<mirror::Class> to,
54 JValue* value)
55 REQUIRES_SHARED(Locks::mutator_lock_);
56
57// Converts the value of the argument at position |index| from type
58// expected by |callee_type| to type used by |callsite_type|. |value|
59// represents the value to be converted. Returns true on success and
60// updates |value|, false otherwise.
61ALWAYS_INLINE bool ConvertArgumentValue(Handle<mirror::MethodType> callsite_type,
62 Handle<mirror::MethodType> callee_type,
63 int index,
64 JValue* value)
65 REQUIRES_SHARED(Locks::mutator_lock_);
66
67// Converts the return value from return type yielded by
68// |callee_type| to the return type yielded by
69// |callsite_type|. |value| represents the value to be
70// converted. Returns true on success and updates |value|, false
71// otherwise.
72ALWAYS_INLINE bool ConvertReturnValue(Handle<mirror::MethodType> callsite_type,
73 Handle<mirror::MethodType> callee_type,
74 JValue* value)
75 REQUIRES_SHARED(Locks::mutator_lock_);
Narayan Kamathda246502016-10-20 18:39:22 +010076
Narayan Kamath208f8572016-08-03 12:46:58 +010077// Perform argument conversions between |callsite_type| (the type of the
78// incoming arguments) and |callee_type| (the type of the method being
79// invoked). These include widening and narrowing conversions as well as
80// boxing and unboxing. Returns true on success, on false on failure. A
81// pending exception will always be set on failure.
Narayan Kamath000e1882016-10-24 17:14:25 +010082//
83// The values to be converted are read from an input source (of type G)
84// that provides three methods :
85//
86// class G {
87// // Used to read the next boolean/short/int or float value from the
88// // source.
89// uint32_t Get();
90//
91// // Used to the read the next reference value from the source.
92// ObjPtr<mirror::Object> GetReference();
93//
94// // Used to read the next double or long value from the source.
95// int64_t GetLong();
96// }
97//
98// After conversion, the values are written to an output sink (of type S)
99// that provides three methods :
100//
101// class S {
102// void Set(uint32_t);
103// void SetReference(ObjPtr<mirror::Object>)
104// void SetLong(int64_t);
105// }
106//
107// The semantics and usage of the Set methods are analagous to the getter
108// class.
109//
110// This method is instantiated in three different scenarions :
111// - <S = ShadowFrameSetter, G = ShadowFrameGetter> : copying from shadow
112// frame to shadow frame, used in a regular polymorphic non-exact invoke.
113// - <S = EmulatedShadowFrameAccessor, G = ShadowFrameGetter> : entering into
114// a transformer method from a polymorphic invoke.
115// - <S = ShadowFrameStter, G = EmulatedStackFrameAccessor> : entering into
116// a regular poly morphic invoke from a transformer method.
117//
118// TODO(narayan): If we find that the instantiations of this function take
119// up too much space, we can make G / S abstract base classes that are
120// overridden by concrete classes.
121template <typename G, typename S>
Narayan Kamath000e1882016-10-24 17:14:25 +0100122bool PerformConversions(Thread* self,
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100123 Handle<mirror::MethodType> callsite_type,
124 Handle<mirror::MethodType> callee_type,
Narayan Kamath000e1882016-10-24 17:14:25 +0100125 G* getter,
126 S* setter,
Narayan Kamath2cb856c2016-11-02 12:01:26 +0000127 int32_t num_conversions) REQUIRES_SHARED(Locks::mutator_lock_);
Narayan Kamath000e1882016-10-24 17:14:25 +0100128
Narayan Kamath000e1882016-10-24 17:14:25 +0100129// A convenience class that allows for iteration through a list of
130// input argument registers |arg| for non-range invokes or a list of
131// consecutive registers starting with a given based for range
132// invokes.
133//
134// This is used to iterate over input arguments while performing standard
135// argument conversions.
Orion Hodson811bd5f2016-12-07 11:35:37 +0000136template <bool is_range>
137class ShadowFrameGetter {
Narayan Kamath000e1882016-10-24 17:14:25 +0100138 public:
139 ShadowFrameGetter(size_t first_src_reg,
140 const uint32_t (&arg)[Instruction::kMaxVarArgRegs],
141 const ShadowFrame& shadow_frame) :
142 first_src_reg_(first_src_reg),
143 arg_(arg),
144 shadow_frame_(shadow_frame),
145 arg_index_(0) {
146 }
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100147
Narayan Kamath000e1882016-10-24 17:14:25 +0100148 ALWAYS_INLINE uint32_t Get() REQUIRES_SHARED(Locks::mutator_lock_) {
149 const uint32_t next = (is_range ? first_src_reg_ + arg_index_ : arg_[arg_index_]);
150 ++arg_index_;
151
152 return shadow_frame_.GetVReg(next);
153 }
154
155 ALWAYS_INLINE int64_t GetLong() REQUIRES_SHARED(Locks::mutator_lock_) {
156 const uint32_t next = (is_range ? first_src_reg_ + arg_index_ : arg_[arg_index_]);
157 arg_index_ += 2;
158
159 return shadow_frame_.GetVRegLong(next);
160 }
161
162 ALWAYS_INLINE ObjPtr<mirror::Object> GetReference() REQUIRES_SHARED(Locks::mutator_lock_) {
163 const uint32_t next = (is_range ? first_src_reg_ + arg_index_ : arg_[arg_index_]);
164 ++arg_index_;
165
166 return shadow_frame_.GetVRegReference(next);
167 }
168
169 private:
170 const size_t first_src_reg_;
171 const uint32_t (&arg_)[Instruction::kMaxVarArgRegs];
172 const ShadowFrame& shadow_frame_;
173 size_t arg_index_;
174};
175
176// A convenience class that allows values to be written to a given shadow frame,
177// starting at location |first_dst_reg|.
178class ShadowFrameSetter {
179 public:
180 ShadowFrameSetter(ShadowFrame* shadow_frame,
181 size_t first_dst_reg) :
182 shadow_frame_(shadow_frame),
183 arg_index_(first_dst_reg) {
184 }
185
186 ALWAYS_INLINE void Set(uint32_t value) REQUIRES_SHARED(Locks::mutator_lock_) {
187 shadow_frame_->SetVReg(arg_index_++, value);
188 }
189
190 ALWAYS_INLINE void SetReference(ObjPtr<mirror::Object> value)
191 REQUIRES_SHARED(Locks::mutator_lock_) {
192 shadow_frame_->SetVRegReference(arg_index_++, value.Ptr());
193 }
194
195 ALWAYS_INLINE void SetLong(int64_t value) REQUIRES_SHARED(Locks::mutator_lock_) {
196 shadow_frame_->SetVRegLong(arg_index_, value);
197 arg_index_ += 2;
198 }
199
200 private:
201 ShadowFrame* shadow_frame_;
202 size_t arg_index_;
203};
Narayan Kamath208f8572016-08-03 12:46:58 +0100204
Orion Hodsonc069a302017-01-18 09:23:12 +0000205template <bool is_range>
Orion Hodson811bd5f2016-12-07 11:35:37 +0000206bool DoInvokePolymorphic(Thread* self,
207 ArtMethod* invoke_method,
208 ShadowFrame& shadow_frame,
Orion Hodsonc069a302017-01-18 09:23:12 +0000209 Handle<mirror::MethodHandle> method_handle,
Orion Hodson811bd5f2016-12-07 11:35:37 +0000210 Handle<mirror::MethodType> callsite_type,
211 const uint32_t (&args)[Instruction::kMaxVarArgRegs],
212 uint32_t first_arg,
213 JValue* result)
214 REQUIRES_SHARED(Locks::mutator_lock_);
215
Narayan Kamath9823e782016-08-03 12:46:58 +0100216} // namespace art
217
218#endif // ART_RUNTIME_METHOD_HANDLES_H_