xref: /aosp_15_r20/art/runtime/imtable-inl.h (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2016 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #ifndef ART_RUNTIME_IMTABLE_INL_H_
18*795d594fSAndroid Build Coastguard Worker #define ART_RUNTIME_IMTABLE_INL_H_
19*795d594fSAndroid Build Coastguard Worker 
20*795d594fSAndroid Build Coastguard Worker #include "imtable.h"
21*795d594fSAndroid Build Coastguard Worker 
22*795d594fSAndroid Build Coastguard Worker #include "art_method-inl.h"
23*795d594fSAndroid Build Coastguard Worker #include "dex/dex_file.h"
24*795d594fSAndroid Build Coastguard Worker #include "dex/utf.h"
25*795d594fSAndroid Build Coastguard Worker 
26*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
27*795d594fSAndroid Build Coastguard Worker 
28*795d594fSAndroid Build Coastguard Worker static constexpr bool kImTableHashUseName = true;
29*795d594fSAndroid Build Coastguard Worker static constexpr bool kImTableHashUseCoefficients = true;
30*795d594fSAndroid Build Coastguard Worker 
31*795d594fSAndroid Build Coastguard Worker // Magic configuration that minimizes some common runtime calls.
32*795d594fSAndroid Build Coastguard Worker static constexpr uint32_t kImTableHashCoefficientClass = 427;
33*795d594fSAndroid Build Coastguard Worker static constexpr uint32_t kImTableHashCoefficientName = 16;
34*795d594fSAndroid Build Coastguard Worker static constexpr uint32_t kImTableHashCoefficientSignature = 14;
35*795d594fSAndroid Build Coastguard Worker 
GetImtHashComponents(ArtMethod * method,uint32_t * class_hash,uint32_t * name_hash,uint32_t * signature_hash)36*795d594fSAndroid Build Coastguard Worker inline void ImTable::GetImtHashComponents(ArtMethod* method,
37*795d594fSAndroid Build Coastguard Worker                                           uint32_t* class_hash,
38*795d594fSAndroid Build Coastguard Worker                                           uint32_t* name_hash,
39*795d594fSAndroid Build Coastguard Worker                                           uint32_t* signature_hash) {
40*795d594fSAndroid Build Coastguard Worker   if (kImTableHashUseName) {
41*795d594fSAndroid Build Coastguard Worker     if (method->IsProxyMethod()) {
42*795d594fSAndroid Build Coastguard Worker       *class_hash = 0;
43*795d594fSAndroid Build Coastguard Worker       *name_hash = 0;
44*795d594fSAndroid Build Coastguard Worker       *signature_hash = 0;
45*795d594fSAndroid Build Coastguard Worker       return;
46*795d594fSAndroid Build Coastguard Worker     }
47*795d594fSAndroid Build Coastguard Worker 
48*795d594fSAndroid Build Coastguard Worker     const DexFile* dex_file = method->GetDexFile();
49*795d594fSAndroid Build Coastguard Worker     const dex::MethodId& method_id = dex_file->GetMethodId(method->GetDexMethodIndex());
50*795d594fSAndroid Build Coastguard Worker 
51*795d594fSAndroid Build Coastguard Worker     // Class descriptor for the class component.
52*795d594fSAndroid Build Coastguard Worker     *class_hash = ComputeModifiedUtf8Hash(dex_file->GetMethodDeclaringClassDescriptor(method_id));
53*795d594fSAndroid Build Coastguard Worker 
54*795d594fSAndroid Build Coastguard Worker     // Method name for the method component.
55*795d594fSAndroid Build Coastguard Worker     *name_hash = ComputeModifiedUtf8Hash(dex_file->GetMethodName(method_id));
56*795d594fSAndroid Build Coastguard Worker 
57*795d594fSAndroid Build Coastguard Worker     const dex::ProtoId& proto_id = dex_file->GetMethodPrototype(method_id);
58*795d594fSAndroid Build Coastguard Worker 
59*795d594fSAndroid Build Coastguard Worker     // Read the proto for the signature component.
60*795d594fSAndroid Build Coastguard Worker     uint32_t tmp = ComputeModifiedUtf8Hash(
61*795d594fSAndroid Build Coastguard Worker         dex_file->GetTypeDescriptor(dex_file->GetTypeId(proto_id.return_type_idx_)));
62*795d594fSAndroid Build Coastguard Worker 
63*795d594fSAndroid Build Coastguard Worker     // Mix in the argument types.
64*795d594fSAndroid Build Coastguard Worker     // Note: we could consider just using the shorty. This would be faster, at the price of
65*795d594fSAndroid Build Coastguard Worker     //       potential collisions.
66*795d594fSAndroid Build Coastguard Worker     const dex::TypeList* param_types = dex_file->GetProtoParameters(proto_id);
67*795d594fSAndroid Build Coastguard Worker     if (param_types != nullptr) {
68*795d594fSAndroid Build Coastguard Worker       for (size_t i = 0; i != param_types->Size(); ++i) {
69*795d594fSAndroid Build Coastguard Worker         const dex::TypeItem& type = param_types->GetTypeItem(i);
70*795d594fSAndroid Build Coastguard Worker         tmp = 31 * tmp + ComputeModifiedUtf8Hash(
71*795d594fSAndroid Build Coastguard Worker             dex_file->GetTypeDescriptor(dex_file->GetTypeId(type.type_idx_)));
72*795d594fSAndroid Build Coastguard Worker       }
73*795d594fSAndroid Build Coastguard Worker     }
74*795d594fSAndroid Build Coastguard Worker 
75*795d594fSAndroid Build Coastguard Worker     *signature_hash = tmp;
76*795d594fSAndroid Build Coastguard Worker     return;
77*795d594fSAndroid Build Coastguard Worker   } else {
78*795d594fSAndroid Build Coastguard Worker     *class_hash = method->GetDexMethodIndex();
79*795d594fSAndroid Build Coastguard Worker     *name_hash = 0;
80*795d594fSAndroid Build Coastguard Worker     *signature_hash = 0;
81*795d594fSAndroid Build Coastguard Worker     return;
82*795d594fSAndroid Build Coastguard Worker   }
83*795d594fSAndroid Build Coastguard Worker }
84*795d594fSAndroid Build Coastguard Worker 
GetImtIndex(ArtMethod * method)85*795d594fSAndroid Build Coastguard Worker inline uint32_t ImTable::GetImtIndex(ArtMethod* method) {
86*795d594fSAndroid Build Coastguard Worker   DCHECK(!method->IsCopied());
87*795d594fSAndroid Build Coastguard Worker   if (!method->IsAbstract()) {
88*795d594fSAndroid Build Coastguard Worker     // For default methods, where we cannot store the imt_index, we use the
89*795d594fSAndroid Build Coastguard Worker     // method_index instead. We mask it with the closest power of two to
90*795d594fSAndroid Build Coastguard Worker     // simplify the interpreter.
91*795d594fSAndroid Build Coastguard Worker     return method->GetMethodIndex() & (ImTable::kSizeTruncToPowerOfTwo - 1);
92*795d594fSAndroid Build Coastguard Worker   }
93*795d594fSAndroid Build Coastguard Worker   uint32_t class_hash, name_hash, signature_hash;
94*795d594fSAndroid Build Coastguard Worker   GetImtHashComponents(method, &class_hash, &name_hash, &signature_hash);
95*795d594fSAndroid Build Coastguard Worker 
96*795d594fSAndroid Build Coastguard Worker   uint32_t mixed_hash;
97*795d594fSAndroid Build Coastguard Worker   if (!kImTableHashUseCoefficients) {
98*795d594fSAndroid Build Coastguard Worker     mixed_hash = class_hash + name_hash + signature_hash;
99*795d594fSAndroid Build Coastguard Worker   } else {
100*795d594fSAndroid Build Coastguard Worker     mixed_hash = kImTableHashCoefficientClass * class_hash +
101*795d594fSAndroid Build Coastguard Worker                  kImTableHashCoefficientName * name_hash +
102*795d594fSAndroid Build Coastguard Worker                  kImTableHashCoefficientSignature * signature_hash;
103*795d594fSAndroid Build Coastguard Worker   }
104*795d594fSAndroid Build Coastguard Worker 
105*795d594fSAndroid Build Coastguard Worker   return mixed_hash % ImTable::kSize;
106*795d594fSAndroid Build Coastguard Worker }
107*795d594fSAndroid Build Coastguard Worker 
108*795d594fSAndroid Build Coastguard Worker }  // namespace art
109*795d594fSAndroid Build Coastguard Worker 
110*795d594fSAndroid Build Coastguard Worker #endif  // ART_RUNTIME_IMTABLE_INL_H_
111*795d594fSAndroid Build Coastguard Worker 
112