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