• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "runtime/include/class_helper.h"
17 
18 #include <algorithm>
19 
20 #include "libpandabase/mem/mem.h"
21 #include "libpandabase/utils/bit_utils.h"
22 #include "runtime/include/class.h"
23 #include "runtime/include/coretypes/tagged_value.h"
24 #include "runtime/include/mem/panda_string.h"
25 
26 namespace panda {
27 
Pad(size_t size,size_t * padding,size_t * n)28 static void Pad(size_t size, size_t *padding, size_t *n)
29 {
30     while (*padding >= size && *n > 0) {
31         *padding -= size;
32         *n -= 1;
33     }
34 }
35 
36 /* static */
ComputeClassSize(size_t vtable_size,size_t imt_size,size_t num_8bit_sfields,size_t num_16bit_sfields,size_t num_32bit_sfields,size_t num_64bit_sfields,size_t num_ref_sfields,size_t num_tagged_sfields)37 size_t ClassHelper::ComputeClassSize(size_t vtable_size, size_t imt_size, size_t num_8bit_sfields,
38                                      size_t num_16bit_sfields, size_t num_32bit_sfields, size_t num_64bit_sfields,
39                                      size_t num_ref_sfields, size_t num_tagged_sfields)
40 {
41     size_t size = sizeof(Class);
42     size = AlignUp(size, OBJECT_POINTER_SIZE);
43     size += vtable_size * POINTER_SIZE;
44     size += imt_size * POINTER_SIZE;
45     size += num_ref_sfields * OBJECT_POINTER_SIZE;
46 
47     constexpr size_t SIZE_64 = sizeof(uint64_t);
48     constexpr size_t SIZE_32 = sizeof(uint32_t);
49     constexpr size_t SIZE_16 = sizeof(uint16_t);
50     constexpr size_t SIZE_8 = sizeof(uint8_t);
51 
52     // Try to fill alignment gaps with fields that have smaller size from largest to smallests
53     static_assert(coretypes::TaggedValue::TaggedTypeSize() == SIZE_64,
54                   "Please fix alignment of the fields of type \"TaggedValue\"");
55     if (!IsAligned<SIZE_64>(size) && (num_64bit_sfields > 0 || num_tagged_sfields > 0)) {
56         size_t padding = AlignUp(size, SIZE_64) - size;
57         size += padding;
58 
59         Pad(SIZE_32, &padding, &num_32bit_sfields);
60         Pad(SIZE_16, &padding, &num_16bit_sfields);
61         Pad(SIZE_8, &padding, &num_8bit_sfields);
62     }
63 
64     if (!IsAligned<SIZE_32>(size) && num_32bit_sfields > 0) {
65         size_t padding = AlignUp(size, SIZE_32) - size;
66         size += padding;
67 
68         Pad(SIZE_16, &padding, &num_16bit_sfields);
69         Pad(SIZE_8, &padding, &num_8bit_sfields);
70     }
71 
72     if (!IsAligned<SIZE_16>(size) && num_16bit_sfields > 0) {
73         size_t padding = AlignUp(size, SIZE_16) - size;
74         size += padding;
75 
76         Pad(SIZE_8, &padding, &num_8bit_sfields);
77     }
78 
79     size += num_64bit_sfields * SIZE_64 + num_32bit_sfields * SIZE_32 + num_16bit_sfields * SIZE_16 +
80             num_8bit_sfields * SIZE_8 + num_tagged_sfields * coretypes::TaggedValue::TaggedTypeSize();
81 
82     return size;
83 }
84 
85 /* static */
GetDescriptor(const uint8_t * name,PandaString * storage)86 const uint8_t *ClassHelper::GetDescriptor(const uint8_t *name, PandaString *storage)
87 {
88     return GetArrayDescriptor(name, 0, storage);
89 }
90 
91 /* static */
GetArrayDescriptor(const uint8_t * component_name,size_t rank,PandaString * storage)92 const uint8_t *ClassHelper::GetArrayDescriptor(const uint8_t *component_name, size_t rank, PandaString *storage)
93 {
94     storage->clear();
95     storage->append(rank, '[');
96     storage->push_back('L');
97     storage->append(utf::Mutf8AsCString(component_name));
98     storage->push_back(';');
99     std::replace(storage->begin(), storage->end(), '.', '/');
100     return utf::CStringAsMutf8(storage->c_str());
101 }
102 
103 /* static */
GetPrimitiveTypeDescriptorChar(panda_file::Type::TypeId type_id)104 char ClassHelper::GetPrimitiveTypeDescriptorChar(panda_file::Type::TypeId type_id)
105 {
106     // static_cast isn't necessary in most implementations but may be by standard
107     return static_cast<char>(*GetPrimitiveTypeDescriptorStr(type_id));
108 }
109 
110 /* static */
GetPrimitiveTypeDescriptorStr(panda_file::Type::TypeId type_id)111 const uint8_t *ClassHelper::GetPrimitiveTypeDescriptorStr(panda_file::Type::TypeId type_id)
112 {
113     if (type_id == panda_file::Type::TypeId::REFERENCE) {
114         UNREACHABLE();
115         return nullptr;
116     }
117 
118     return utf::CStringAsMutf8(panda_file::Type::GetSignatureByTypeId(panda_file::Type(type_id)));
119 }
120 
121 /* static */
GetPrimitiveTypeStr(panda_file::Type::TypeId type_id)122 const char *ClassHelper::GetPrimitiveTypeStr(panda_file::Type::TypeId type_id)
123 {
124     switch (type_id) {
125         case panda_file::Type::TypeId::VOID:
126             return "void";
127         case panda_file::Type::TypeId::U1:
128             return "bool";
129         case panda_file::Type::TypeId::I8:
130             return "i8";
131         case panda_file::Type::TypeId::U8:
132             return "u8";
133         case panda_file::Type::TypeId::I16:
134             return "i16";
135         case panda_file::Type::TypeId::U16:
136             return "u16";
137         case panda_file::Type::TypeId::I32:
138             return "i32";
139         case panda_file::Type::TypeId::U32:
140             return "u32";
141         case panda_file::Type::TypeId::I64:
142             return "i64";
143         case panda_file::Type::TypeId::U64:
144             return "u64";
145         case panda_file::Type::TypeId::F32:
146             return "f32";
147         case panda_file::Type::TypeId::F64:
148             return "f64";
149         default:
150             UNREACHABLE();
151             break;
152     }
153     return nullptr;
154 }
155 
156 /* static */
GetPrimitiveDescriptor(panda_file::Type type,PandaString * storage)157 const uint8_t *ClassHelper::GetPrimitiveDescriptor(panda_file::Type type, PandaString *storage)
158 {
159     return GetPrimitiveArrayDescriptor(type, 0, storage);
160 }
161 
162 /* static */
GetPrimitiveArrayDescriptor(panda_file::Type type,size_t rank,PandaString * storage)163 const uint8_t *ClassHelper::GetPrimitiveArrayDescriptor(panda_file::Type type, size_t rank, PandaString *storage)
164 {
165     storage->clear();
166     storage->append(rank, '[');
167     storage->push_back(GetPrimitiveTypeDescriptorChar(type.GetId()));
168     return utf::CStringAsMutf8(storage->c_str());
169 }
170 
171 /* static */
GetTypeDescriptor(const PandaString & name,PandaString * storage)172 const uint8_t *ClassHelper::GetTypeDescriptor(const PandaString &name, PandaString *storage)
173 {
174     *storage = "L" + name + ";";
175     std::replace(storage->begin(), storage->end(), '.', '/');
176     return utf::CStringAsMutf8(storage->c_str());
177 }
178 
179 }  // namespace panda
180