blob: 0cc69f20464c0e612db56184be36baac996367de [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 Hodsonba28f9f2016-10-26 10:56:25 +010062// Performs a conversion from type |from| to a distinct type |to| as
63// part of conversion of |caller_type| to |callee_type|. The value to
64// be converted is in |value|. Returns true on success and updates
65// |value| with the converted value, false otherwise.
66bool ConvertJValueCommon(Handle<mirror::MethodType> callsite_type,
67 Handle<mirror::MethodType> callee_type,
68 ObjPtr<mirror::Class> from,
69 ObjPtr<mirror::Class> to,
70 JValue* value)
71 REQUIRES_SHARED(Locks::mutator_lock_);
72
73// Converts the value of the argument at position |index| from type
74// expected by |callee_type| to type used by |callsite_type|. |value|
75// represents the value to be converted. Returns true on success and
76// updates |value|, false otherwise.
77ALWAYS_INLINE bool ConvertArgumentValue(Handle<mirror::MethodType> callsite_type,
78 Handle<mirror::MethodType> callee_type,
79 int index,
80 JValue* value)
81 REQUIRES_SHARED(Locks::mutator_lock_);
82
83// Converts the return value from return type yielded by
84// |callee_type| to the return type yielded by
85// |callsite_type|. |value| represents the value to be
86// converted. Returns true on success and updates |value|, false
87// otherwise.
88ALWAYS_INLINE bool ConvertReturnValue(Handle<mirror::MethodType> callsite_type,
89 Handle<mirror::MethodType> callee_type,
90 JValue* value)
91 REQUIRES_SHARED(Locks::mutator_lock_);
Narayan Kamathda246502016-10-20 18:39:22 +010092
Narayan Kamath208f8572016-08-03 12:46:58 +010093// Perform argument conversions between |callsite_type| (the type of the
94// incoming arguments) and |callee_type| (the type of the method being
95// invoked). These include widening and narrowing conversions as well as
96// boxing and unboxing. Returns true on success, on false on failure. A
97// pending exception will always be set on failure.
Narayan Kamath000e1882016-10-24 17:14:25 +010098//
99// The values to be converted are read from an input source (of type G)
100// that provides three methods :
101//
102// class G {
103// // Used to read the next boolean/short/int or float value from the
104// // source.
105// uint32_t Get();
106//
107// // Used to the read the next reference value from the source.
108// ObjPtr<mirror::Object> GetReference();
109//
110// // Used to read the next double or long value from the source.
111// int64_t GetLong();
112// }
113//
114// After conversion, the values are written to an output sink (of type S)
115// that provides three methods :
116//
117// class S {
118// void Set(uint32_t);
119// void SetReference(ObjPtr<mirror::Object>)
120// void SetLong(int64_t);
121// }
122//
123// The semantics and usage of the Set methods are analagous to the getter
124// class.
125//
126// This method is instantiated in three different scenarions :
127// - <S = ShadowFrameSetter, G = ShadowFrameGetter> : copying from shadow
128// frame to shadow frame, used in a regular polymorphic non-exact invoke.
129// - <S = EmulatedShadowFrameAccessor, G = ShadowFrameGetter> : entering into
130// a transformer method from a polymorphic invoke.
131// - <S = ShadowFrameStter, G = EmulatedStackFrameAccessor> : entering into
132// a regular poly morphic invoke from a transformer method.
133//
134// TODO(narayan): If we find that the instantiations of this function take
135// up too much space, we can make G / S abstract base classes that are
136// overridden by concrete classes.
137template <typename G, typename S>
Narayan Kamath000e1882016-10-24 17:14:25 +0100138bool PerformConversions(Thread* self,
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100139 Handle<mirror::MethodType> callsite_type,
140 Handle<mirror::MethodType> callee_type,
Narayan Kamath000e1882016-10-24 17:14:25 +0100141 G* getter,
142 S* setter,
Narayan Kamath2cb856c2016-11-02 12:01:26 +0000143 int32_t num_conversions) REQUIRES_SHARED(Locks::mutator_lock_);
Narayan Kamath000e1882016-10-24 17:14:25 +0100144
145// A convenience wrapper around |PerformConversions|, for the case where
146// the setter and getter are both ShadowFrame based.
Narayan Kamath2cb856c2016-11-02 12:01:26 +0000147template <bool is_range>
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100148bool ConvertAndCopyArgumentsFromCallerFrame(Thread* self,
149 Handle<mirror::MethodType> callsite_type,
150 Handle<mirror::MethodType> callee_type,
151 const ShadowFrame& caller_frame,
152 uint32_t first_src_reg,
153 uint32_t first_dest_reg,
154 const uint32_t (&arg)[Instruction::kMaxVarArgRegs],
Narayan Kamath2cb856c2016-11-02 12:01:26 +0000155 ShadowFrame* callee_frame)
156 REQUIRES_SHARED(Locks::mutator_lock_);
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100157
Narayan Kamath000e1882016-10-24 17:14:25 +0100158// A convenience class that allows for iteration through a list of
159// input argument registers |arg| for non-range invokes or a list of
160// consecutive registers starting with a given based for range
161// invokes.
162//
163// This is used to iterate over input arguments while performing standard
164// argument conversions.
165template <bool is_range> class ShadowFrameGetter {
166 public:
167 ShadowFrameGetter(size_t first_src_reg,
168 const uint32_t (&arg)[Instruction::kMaxVarArgRegs],
169 const ShadowFrame& shadow_frame) :
170 first_src_reg_(first_src_reg),
171 arg_(arg),
172 shadow_frame_(shadow_frame),
173 arg_index_(0) {
174 }
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100175
Narayan Kamath000e1882016-10-24 17:14:25 +0100176 ALWAYS_INLINE uint32_t Get() REQUIRES_SHARED(Locks::mutator_lock_) {
177 const uint32_t next = (is_range ? first_src_reg_ + arg_index_ : arg_[arg_index_]);
178 ++arg_index_;
179
180 return shadow_frame_.GetVReg(next);
181 }
182
183 ALWAYS_INLINE int64_t GetLong() REQUIRES_SHARED(Locks::mutator_lock_) {
184 const uint32_t next = (is_range ? first_src_reg_ + arg_index_ : arg_[arg_index_]);
185 arg_index_ += 2;
186
187 return shadow_frame_.GetVRegLong(next);
188 }
189
190 ALWAYS_INLINE ObjPtr<mirror::Object> GetReference() REQUIRES_SHARED(Locks::mutator_lock_) {
191 const uint32_t next = (is_range ? first_src_reg_ + arg_index_ : arg_[arg_index_]);
192 ++arg_index_;
193
194 return shadow_frame_.GetVRegReference(next);
195 }
196
197 private:
198 const size_t first_src_reg_;
199 const uint32_t (&arg_)[Instruction::kMaxVarArgRegs];
200 const ShadowFrame& shadow_frame_;
201 size_t arg_index_;
202};
203
204// A convenience class that allows values to be written to a given shadow frame,
205// starting at location |first_dst_reg|.
206class ShadowFrameSetter {
207 public:
208 ShadowFrameSetter(ShadowFrame* shadow_frame,
209 size_t first_dst_reg) :
210 shadow_frame_(shadow_frame),
211 arg_index_(first_dst_reg) {
212 }
213
214 ALWAYS_INLINE void Set(uint32_t value) REQUIRES_SHARED(Locks::mutator_lock_) {
215 shadow_frame_->SetVReg(arg_index_++, value);
216 }
217
218 ALWAYS_INLINE void SetReference(ObjPtr<mirror::Object> value)
219 REQUIRES_SHARED(Locks::mutator_lock_) {
220 shadow_frame_->SetVRegReference(arg_index_++, value.Ptr());
221 }
222
223 ALWAYS_INLINE void SetLong(int64_t value) REQUIRES_SHARED(Locks::mutator_lock_) {
224 shadow_frame_->SetVRegLong(arg_index_, value);
225 arg_index_ += 2;
226 }
227
228 private:
229 ShadowFrame* shadow_frame_;
230 size_t arg_index_;
231};
Narayan Kamath208f8572016-08-03 12:46:58 +0100232
Narayan Kamath9823e782016-08-03 12:46:58 +0100233} // namespace art
234
235#endif // ART_RUNTIME_METHOD_HANDLES_H_