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 #include "method_type-inl.h"
18
19 #include "class-alloc-inl.h"
20 #include "class_root.h"
21 #include "method_handles.h"
22 #include "obj_ptr-inl.h"
23 #include "object_array-alloc-inl.h"
24 #include "object_array-inl.h"
25
26 namespace art {
27 namespace mirror {
28
29 namespace {
30
AllocatePTypesArray(Thread * self,int count)31 ObjPtr<ObjectArray<Class>> AllocatePTypesArray(Thread* self, int count)
32 REQUIRES_SHARED(Locks::mutator_lock_) {
33 ObjPtr<Class> class_array_type = GetClassRoot<mirror::ObjectArray<mirror::Class>>();
34 return ObjectArray<Class>::Alloc(self, class_array_type, count);
35 }
36
37 } // namespace
38
Create(Thread * const self,Handle<Class> return_type,Handle<ObjectArray<Class>> parameter_types)39 ObjPtr<MethodType> MethodType::Create(Thread* const self,
40 Handle<Class> return_type,
41 Handle<ObjectArray<Class>> parameter_types) {
42 StackHandleScope<1> hs(self);
43 Handle<MethodType> mt(
44 hs.NewHandle(ObjPtr<MethodType>::DownCast(GetClassRoot<MethodType>()->AllocObject(self))));
45
46 // TODO: Do we ever create a MethodType during a transaction ? There doesn't
47 // seem like a good reason to do a polymorphic invoke that results in the
48 // resolution of a method type in an unstarted runtime.
49 mt->SetFieldObject<false>(FormOffset(), nullptr);
50 mt->SetFieldObject<false>(MethodDescriptorOffset(), nullptr);
51 mt->SetFieldObject<false>(RTypeOffset(), return_type.Get());
52 mt->SetFieldObject<false>(PTypesOffset(), parameter_types.Get());
53 mt->SetFieldObject<false>(WrapAltOffset(), nullptr);
54
55 return mt.Get();
56 }
57
CloneWithoutLeadingParameter(Thread * const self,ObjPtr<MethodType> method_type)58 ObjPtr<MethodType> MethodType::CloneWithoutLeadingParameter(Thread* const self,
59 ObjPtr<MethodType> method_type) {
60 StackHandleScope<3> hs(self);
61 Handle<ObjectArray<Class>> src_ptypes = hs.NewHandle(method_type->GetPTypes());
62 Handle<Class> dst_rtype = hs.NewHandle(method_type->GetRType());
63 const int32_t dst_ptypes_count = method_type->GetNumberOfPTypes() - 1;
64 Handle<ObjectArray<Class>> dst_ptypes = hs.NewHandle(AllocatePTypesArray(self, dst_ptypes_count));
65 if (dst_ptypes.IsNull()) {
66 return nullptr;
67 }
68 for (int32_t i = 0; i < dst_ptypes_count; ++i) {
69 dst_ptypes->Set(i, src_ptypes->Get(i + 1));
70 }
71 return Create(self, dst_rtype, dst_ptypes);
72 }
73
CollectTrailingArguments(Thread * self,ObjPtr<MethodType> method_type,ObjPtr<Class> collector_array_class,int32_t start_index)74 ObjPtr<MethodType> MethodType::CollectTrailingArguments(Thread* self,
75 ObjPtr<MethodType> method_type,
76 ObjPtr<Class> collector_array_class,
77 int32_t start_index) {
78 int32_t ptypes_length = method_type->GetNumberOfPTypes();
79 if (start_index > ptypes_length) {
80 return method_type;
81 }
82
83 StackHandleScope<4> hs(self);
84 Handle<Class> collector_class = hs.NewHandle(collector_array_class);
85 Handle<Class> dst_rtype = hs.NewHandle(method_type->GetRType());
86 Handle<ObjectArray<Class>> src_ptypes = hs.NewHandle(method_type->GetPTypes());
87 Handle<ObjectArray<Class>> dst_ptypes = hs.NewHandle(AllocatePTypesArray(self, start_index + 1));
88 if (dst_ptypes.IsNull()) {
89 return nullptr;
90 }
91 for (int32_t i = 0; i < start_index; ++i) {
92 dst_ptypes->Set(i, src_ptypes->Get(i));
93 }
94 dst_ptypes->Set(start_index, collector_class.Get());
95 return Create(self, dst_rtype, dst_ptypes);
96 }
97
NumberOfVRegs()98 size_t MethodType::NumberOfVRegs() {
99 const ObjPtr<ObjectArray<Class>> p_types = GetPTypes();
100 const int32_t p_types_length = p_types->GetLength();
101
102 // Initialize |num_vregs| with number of parameters and only increment it for
103 // types requiring a second vreg.
104 size_t num_vregs = static_cast<size_t>(p_types_length);
105 for (int32_t i = 0; i < p_types_length; ++i) {
106 ObjPtr<Class> klass = p_types->GetWithoutChecks(i);
107 if (klass->IsPrimitiveLong() || klass->IsPrimitiveDouble()) {
108 ++num_vregs;
109 }
110 }
111 return num_vregs;
112 }
113
IsExactMatch(ObjPtr<MethodType> target)114 bool MethodType::IsExactMatch(ObjPtr<MethodType> target) {
115 const ObjPtr<ObjectArray<Class>> p_types = GetPTypes();
116 const int32_t params_length = p_types->GetLength();
117
118 const ObjPtr<ObjectArray<Class>> target_p_types = target->GetPTypes();
119 if (params_length != target_p_types->GetLength()) {
120 return false;
121 }
122 for (int32_t i = 0; i < params_length; ++i) {
123 if (p_types->GetWithoutChecks(i) != target_p_types->GetWithoutChecks(i)) {
124 return false;
125 }
126 }
127 return GetRType() == target->GetRType();
128 }
129
IsConvertible(ObjPtr<MethodType> target)130 bool MethodType::IsConvertible(ObjPtr<MethodType> target) {
131 const ObjPtr<ObjectArray<Class>> p_types = GetPTypes();
132 const int32_t params_length = p_types->GetLength();
133
134 const ObjPtr<ObjectArray<Class>> target_p_types = target->GetPTypes();
135 if (params_length != target_p_types->GetLength()) {
136 return false;
137 }
138
139 // Perform return check before invoking method handle otherwise side
140 // effects from the invocation may be observable before
141 // WrongMethodTypeException is raised.
142 if (!IsReturnTypeConvertible(target->GetRType(), GetRType())) {
143 return false;
144 }
145
146 for (int32_t i = 0; i < params_length; ++i) {
147 if (!IsParameterTypeConvertible(p_types->GetWithoutChecks(i),
148 target_p_types->GetWithoutChecks(i))) {
149 return false;
150 }
151 }
152 return true;
153 }
154
PrettyDescriptor()155 std::string MethodType::PrettyDescriptor() {
156 std::ostringstream ss;
157 ss << "(";
158
159 const ObjPtr<ObjectArray<Class>> p_types = GetPTypes();
160 const int32_t params_length = p_types->GetLength();
161 for (int32_t i = 0; i < params_length; ++i) {
162 ss << p_types->GetWithoutChecks(i)->PrettyDescriptor();
163 if (i != (params_length - 1)) {
164 ss << ", ";
165 }
166 }
167
168 ss << ")";
169 ss << GetRType()->PrettyDescriptor();
170
171 return ss.str();
172 }
173
174 } // namespace mirror
175 } // namespace art
176