blob: 54c772a4f216d3173c0e6a3cceb7c43695f5f3a3 [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"
26#include "mirror/method_type.h"
Narayan Kamath208f8572016-08-03 12:46:58 +010027
Narayan Kamath9823e782016-08-03 12:46:58 +010028namespace art {
29
Narayan Kamath208f8572016-08-03 12:46:58 +010030namespace mirror {
31 class MethodType;
32}
33
34class ShadowFrame;
35
Narayan Kamath9823e782016-08-03 12:46:58 +010036// Defines the behaviour of a given method handle. The behaviour
37// of a handle of a given kind is identical to the dex bytecode behaviour
38// of the equivalent instruction.
39//
40// NOTE: These must be kept in sync with the constants defined in
41// java.lang.invoke.MethodHandle.
42enum MethodHandleKind {
43 kInvokeVirtual = 0,
44 kInvokeSuper,
45 kInvokeDirect,
46 kInvokeStatic,
47 kInvokeInterface,
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +010048 kInvokeTransform,
Narayan Kamath9823e782016-08-03 12:46:58 +010049 kInstanceGet,
50 kInstancePut,
51 kStaticGet,
52 kStaticPut,
53 kLastValidKind = kStaticPut,
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +010054 kLastInvokeKind = kInvokeTransform
Narayan Kamath9823e782016-08-03 12:46:58 +010055};
56
57// Whether the given method handle kind is some variant of an invoke.
58inline bool IsInvoke(const MethodHandleKind handle_kind) {
59 return handle_kind <= kLastInvokeKind;
60}
61
Orion Hodson1a06f9f2016-11-09 08:32:42 +000062// Returns true if there is a possible conversion from |from| to |to|
63// for a MethodHandle parameter.
64bool IsParameterTypeConvertible(ObjPtr<mirror::Class> from,
65 ObjPtr<mirror::Class> to);
66
67// Returns true if there is a possible conversion from |from| to |to|
68// for the return type of a MethodHandle.
69bool IsReturnTypeConvertible(ObjPtr<mirror::Class> from,
70 ObjPtr<mirror::Class> to);
71
Orion Hodsonba28f9f2016-10-26 10:56:25 +010072// Performs a conversion from type |from| to a distinct type |to| as
73// part of conversion of |caller_type| to |callee_type|. The value to
74// be converted is in |value|. Returns true on success and updates
75// |value| with the converted value, false otherwise.
76bool ConvertJValueCommon(Handle<mirror::MethodType> callsite_type,
77 Handle<mirror::MethodType> callee_type,
78 ObjPtr<mirror::Class> from,
79 ObjPtr<mirror::Class> to,
80 JValue* value)
81 REQUIRES_SHARED(Locks::mutator_lock_);
82
83// Converts the value of the argument at position |index| from type
84// expected by |callee_type| to type used by |callsite_type|. |value|
85// represents the value to be converted. Returns true on success and
86// updates |value|, false otherwise.
87ALWAYS_INLINE bool ConvertArgumentValue(Handle<mirror::MethodType> callsite_type,
88 Handle<mirror::MethodType> callee_type,
89 int index,
90 JValue* value)
91 REQUIRES_SHARED(Locks::mutator_lock_);
92
93// Converts the return value from return type yielded by
94// |callee_type| to the return type yielded by
95// |callsite_type|. |value| represents the value to be
96// converted. Returns true on success and updates |value|, false
97// otherwise.
98ALWAYS_INLINE bool ConvertReturnValue(Handle<mirror::MethodType> callsite_type,
99 Handle<mirror::MethodType> callee_type,
100 JValue* value)
101 REQUIRES_SHARED(Locks::mutator_lock_);
Narayan Kamathda246502016-10-20 18:39:22 +0100102
Narayan Kamath208f8572016-08-03 12:46:58 +0100103// Perform argument conversions between |callsite_type| (the type of the
104// incoming arguments) and |callee_type| (the type of the method being
105// invoked). These include widening and narrowing conversions as well as
106// boxing and unboxing. Returns true on success, on false on failure. A
107// pending exception will always be set on failure.
Narayan Kamath000e1882016-10-24 17:14:25 +0100108//
109// The values to be converted are read from an input source (of type G)
110// that provides three methods :
111//
112// class G {
113// // Used to read the next boolean/short/int or float value from the
114// // source.
115// uint32_t Get();
116//
117// // Used to the read the next reference value from the source.
118// ObjPtr<mirror::Object> GetReference();
119//
120// // Used to read the next double or long value from the source.
121// int64_t GetLong();
122// }
123//
124// After conversion, the values are written to an output sink (of type S)
125// that provides three methods :
126//
127// class S {
128// void Set(uint32_t);
129// void SetReference(ObjPtr<mirror::Object>)
130// void SetLong(int64_t);
131// }
132//
133// The semantics and usage of the Set methods are analagous to the getter
134// class.
135//
136// This method is instantiated in three different scenarions :
137// - <S = ShadowFrameSetter, G = ShadowFrameGetter> : copying from shadow
138// frame to shadow frame, used in a regular polymorphic non-exact invoke.
139// - <S = EmulatedShadowFrameAccessor, G = ShadowFrameGetter> : entering into
140// a transformer method from a polymorphic invoke.
141// - <S = ShadowFrameStter, G = EmulatedStackFrameAccessor> : entering into
142// a regular poly morphic invoke from a transformer method.
143//
144// TODO(narayan): If we find that the instantiations of this function take
145// up too much space, we can make G / S abstract base classes that are
146// overridden by concrete classes.
147template <typename G, typename S>
Narayan Kamath000e1882016-10-24 17:14:25 +0100148bool PerformConversions(Thread* self,
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100149 Handle<mirror::MethodType> callsite_type,
150 Handle<mirror::MethodType> callee_type,
Narayan Kamath000e1882016-10-24 17:14:25 +0100151 G* getter,
152 S* setter,
Narayan Kamath2cb856c2016-11-02 12:01:26 +0000153 int32_t num_conversions) REQUIRES_SHARED(Locks::mutator_lock_);
Narayan Kamath000e1882016-10-24 17:14:25 +0100154
155// A convenience wrapper around |PerformConversions|, for the case where
156// the setter and getter are both ShadowFrame based.
Narayan Kamath2cb856c2016-11-02 12:01:26 +0000157template <bool is_range>
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100158bool ConvertAndCopyArgumentsFromCallerFrame(Thread* self,
159 Handle<mirror::MethodType> callsite_type,
160 Handle<mirror::MethodType> callee_type,
161 const ShadowFrame& caller_frame,
162 uint32_t first_src_reg,
163 uint32_t first_dest_reg,
164 const uint32_t (&arg)[Instruction::kMaxVarArgRegs],
Narayan Kamath2cb856c2016-11-02 12:01:26 +0000165 ShadowFrame* callee_frame)
166 REQUIRES_SHARED(Locks::mutator_lock_);
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100167
Narayan Kamath000e1882016-10-24 17:14:25 +0100168// A convenience class that allows for iteration through a list of
169// input argument registers |arg| for non-range invokes or a list of
170// consecutive registers starting with a given based for range
171// invokes.
172//
173// This is used to iterate over input arguments while performing standard
174// argument conversions.
175template <bool is_range> class ShadowFrameGetter {
176 public:
177 ShadowFrameGetter(size_t first_src_reg,
178 const uint32_t (&arg)[Instruction::kMaxVarArgRegs],
179 const ShadowFrame& shadow_frame) :
180 first_src_reg_(first_src_reg),
181 arg_(arg),
182 shadow_frame_(shadow_frame),
183 arg_index_(0) {
184 }
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100185
Narayan Kamath000e1882016-10-24 17:14:25 +0100186 ALWAYS_INLINE uint32_t Get() REQUIRES_SHARED(Locks::mutator_lock_) {
187 const uint32_t next = (is_range ? first_src_reg_ + arg_index_ : arg_[arg_index_]);
188 ++arg_index_;
189
190 return shadow_frame_.GetVReg(next);
191 }
192
193 ALWAYS_INLINE int64_t GetLong() REQUIRES_SHARED(Locks::mutator_lock_) {
194 const uint32_t next = (is_range ? first_src_reg_ + arg_index_ : arg_[arg_index_]);
195 arg_index_ += 2;
196
197 return shadow_frame_.GetVRegLong(next);
198 }
199
200 ALWAYS_INLINE ObjPtr<mirror::Object> GetReference() REQUIRES_SHARED(Locks::mutator_lock_) {
201 const uint32_t next = (is_range ? first_src_reg_ + arg_index_ : arg_[arg_index_]);
202 ++arg_index_;
203
204 return shadow_frame_.GetVRegReference(next);
205 }
206
207 private:
208 const size_t first_src_reg_;
209 const uint32_t (&arg_)[Instruction::kMaxVarArgRegs];
210 const ShadowFrame& shadow_frame_;
211 size_t arg_index_;
212};
213
214// A convenience class that allows values to be written to a given shadow frame,
215// starting at location |first_dst_reg|.
216class ShadowFrameSetter {
217 public:
218 ShadowFrameSetter(ShadowFrame* shadow_frame,
219 size_t first_dst_reg) :
220 shadow_frame_(shadow_frame),
221 arg_index_(first_dst_reg) {
222 }
223
224 ALWAYS_INLINE void Set(uint32_t value) REQUIRES_SHARED(Locks::mutator_lock_) {
225 shadow_frame_->SetVReg(arg_index_++, value);
226 }
227
228 ALWAYS_INLINE void SetReference(ObjPtr<mirror::Object> value)
229 REQUIRES_SHARED(Locks::mutator_lock_) {
230 shadow_frame_->SetVRegReference(arg_index_++, value.Ptr());
231 }
232
233 ALWAYS_INLINE void SetLong(int64_t value) REQUIRES_SHARED(Locks::mutator_lock_) {
234 shadow_frame_->SetVRegLong(arg_index_, value);
235 arg_index_ += 2;
236 }
237
238 private:
239 ShadowFrame* shadow_frame_;
240 size_t arg_index_;
241};
Narayan Kamath208f8572016-08-03 12:46:58 +0100242
Narayan Kamath9823e782016-08-03 12:46:58 +0100243} // namespace art
244
245#endif // ART_RUNTIME_METHOD_HANDLES_H_