blob: bc74bf23d2758a66977da7dd02b1ec289fdedff5 [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 {
Igor Murashkin2ffb7032017-11-08 13:35:21 -080031class MethodHandle;
32class 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
Orion Hodson960d4f72017-11-10 15:32:38 +0000129// input argument registers. This is used to iterate over input
130// arguments while performing standard argument conversions.
Orion Hodson811bd5f2016-12-07 11:35:37 +0000131class ShadowFrameGetter {
Narayan Kamath000e1882016-10-24 17:14:25 +0100132 public:
Orion Hodson960d4f72017-11-10 15:32:38 +0000133 ShadowFrameGetter(const InstructionOperands* const operands, const ShadowFrame& shadow_frame)
134 : operands_(operands), operand_index_(0), shadow_frame_(shadow_frame) {}
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100135
Narayan Kamath000e1882016-10-24 17:14:25 +0100136 ALWAYS_INLINE uint32_t Get() REQUIRES_SHARED(Locks::mutator_lock_) {
Orion Hodson960d4f72017-11-10 15:32:38 +0000137 return shadow_frame_.GetVReg(Next());
Narayan Kamath000e1882016-10-24 17:14:25 +0100138 }
139
140 ALWAYS_INLINE int64_t GetLong() REQUIRES_SHARED(Locks::mutator_lock_) {
Orion Hodson960d4f72017-11-10 15:32:38 +0000141 return shadow_frame_.GetVRegLong(NextLong());
Narayan Kamath000e1882016-10-24 17:14:25 +0100142 }
143
144 ALWAYS_INLINE ObjPtr<mirror::Object> GetReference() REQUIRES_SHARED(Locks::mutator_lock_) {
Orion Hodson960d4f72017-11-10 15:32:38 +0000145 return shadow_frame_.GetVRegReference(Next());
Narayan Kamath000e1882016-10-24 17:14:25 +0100146 }
147
148 private:
Orion Hodson960d4f72017-11-10 15:32:38 +0000149 uint32_t Next() {
150 const uint32_t next = operands_->GetOperand(operand_index_);
151 operand_index_ += 1;
152 return next;
153 }
154 uint32_t NextLong() {
155 const uint32_t next = operands_->GetOperand(operand_index_);
156 operand_index_ += 2;
157 return next;
158 }
159
160 const InstructionOperands* const operands_;
161 size_t operand_index_; // the next register operand to read from frame
Narayan Kamath000e1882016-10-24 17:14:25 +0100162 const ShadowFrame& shadow_frame_;
Narayan Kamath000e1882016-10-24 17:14:25 +0100163};
164
165// A convenience class that allows values to be written to a given shadow frame,
166// starting at location |first_dst_reg|.
167class ShadowFrameSetter {
168 public:
169 ShadowFrameSetter(ShadowFrame* shadow_frame,
170 size_t first_dst_reg) :
171 shadow_frame_(shadow_frame),
172 arg_index_(first_dst_reg) {
173 }
174
175 ALWAYS_INLINE void Set(uint32_t value) REQUIRES_SHARED(Locks::mutator_lock_) {
176 shadow_frame_->SetVReg(arg_index_++, value);
177 }
178
179 ALWAYS_INLINE void SetReference(ObjPtr<mirror::Object> value)
180 REQUIRES_SHARED(Locks::mutator_lock_) {
181 shadow_frame_->SetVRegReference(arg_index_++, value.Ptr());
182 }
183
184 ALWAYS_INLINE void SetLong(int64_t value) REQUIRES_SHARED(Locks::mutator_lock_) {
185 shadow_frame_->SetVRegLong(arg_index_, value);
186 arg_index_ += 2;
187 }
188
189 private:
190 ShadowFrame* shadow_frame_;
191 size_t arg_index_;
192};
Narayan Kamath208f8572016-08-03 12:46:58 +0100193
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100194bool MethodHandleInvoke(Thread* self,
195 ShadowFrame& shadow_frame,
196 Handle<mirror::MethodHandle> method_handle,
197 Handle<mirror::MethodType> callsite_type,
Orion Hodson960d4f72017-11-10 15:32:38 +0000198 const InstructionOperands* const args,
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100199 JValue* result)
200 REQUIRES_SHARED(Locks::mutator_lock_);
201
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100202bool MethodHandleInvokeExact(Thread* self,
203 ShadowFrame& shadow_frame,
204 Handle<mirror::MethodHandle> method_handle,
205 Handle<mirror::MethodType> callsite_type,
Orion Hodson960d4f72017-11-10 15:32:38 +0000206 const InstructionOperands* const args,
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100207 JValue* result)
Orion Hodson811bd5f2016-12-07 11:35:37 +0000208 REQUIRES_SHARED(Locks::mutator_lock_);
209
Narayan Kamath9823e782016-08-03 12:46:58 +0100210} // namespace art
211
212#endif // ART_RUNTIME_METHOD_HANDLES_H_