1 /*
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 
22 #include "dex/dex_instruction.h"
23 #include "handle.h"
24 #include "jvalue.h"
25 #include "mirror/class.h"
26 
27 namespace art {
28 
29 class ShadowFrame;
30 
31 namespace mirror {
32 class MethodHandle;
33 class MethodType;
34 }  // namespace mirror
35 
36 // Returns true if there is a possible conversion from |from| to |to|
37 // for a MethodHandle parameter.
38 bool 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.
43 bool IsReturnTypeConvertible(ObjPtr<mirror::Class> from,
44                              ObjPtr<mirror::Class> to);
45 
46 // 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.
50 bool 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.
61 ALWAYS_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.
72 ALWAYS_INLINE bool ConvertReturnValue(Handle<mirror::MethodType> callsite_type,
73                                       Handle<mirror::MethodType> callee_type,
74                                       JValue* value)
75     REQUIRES_SHARED(Locks::mutator_lock_);
76 
77 // 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.
82 //
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.
121 template <typename G, typename S>
122 bool PerformConversions(Thread* self,
123                         Handle<mirror::MethodType> callsite_type,
124                         Handle<mirror::MethodType> callee_type,
125                         G* getter,
126                         S* setter,
127                         int32_t start_index,
128                         int32_t end_index) REQUIRES_SHARED(Locks::mutator_lock_);
129 
130 bool MethodHandleInvoke(Thread* self,
131                         ShadowFrame& shadow_frame,
132                         Handle<mirror::MethodHandle> method_handle,
133                         Handle<mirror::MethodType> callsite_type,
134                         const InstructionOperands* const args,
135                         JValue* result)
136     REQUIRES_SHARED(Locks::mutator_lock_);
137 
138 bool MethodHandleInvokeExact(Thread* self,
139                              ShadowFrame& shadow_frame,
140                              Handle<mirror::MethodHandle> method_handle,
141                              Handle<mirror::MethodType> callsite_type,
142                              const InstructionOperands* const args,
143                              JValue* result)
144     REQUIRES_SHARED(Locks::mutator_lock_);
145 
146 }  // namespace art
147 
148 #endif  // ART_RUNTIME_METHOD_HANDLES_H_
149