1 /*
2 * Copyright (c) 2021-2022 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 <utility>
17
18 #include "type_system.h"
19 #include "type_sort.h"
20 #include "type_image.h"
21 #include "type_params.h"
22 #include "type_tags.h"
23 #include "type_systems.h"
24
25 #include "verification/util/enum_array.h"
26 #include "verification/util/hash.h"
27
28 #include "runtime/include/mem/allocator.h"
29 #include "runtime/include/mem/panda_containers.h"
30
31 #include "macros.h"
32
33 namespace panda::verifier {
34
35 class FullTypeSystem {
36 public:
FullTypeSystem(TypeSystemKind kind,ThreadNum threadnum)37 explicit FullTypeSystem(TypeSystemKind kind, ThreadNum threadnum)
38 : sort_ {"Bot", "Top"},
39 type_image_ {sort_},
40 bot_sort_ {sort_["Bot"]},
41 top_sort_ {sort_["Top"]},
42 type_system_ {bot_sort_, top_sort_, threadnum, kind}
43 {
44 }
45 NO_COPY_SEMANTIC(FullTypeSystem);
46 // can't use the default move constructor due to an internal pointer in type_image_
FullTypeSystem(FullTypeSystem && other)47 FullTypeSystem(FullTypeSystem &&other)
48 : sort_ {std::move(other.sort_)},
49 type_image_ {sort_},
50 bot_sort_ {std::exchange(other.bot_sort_, 0)},
51 top_sort_ {std::exchange(other.top_sort_, 1)},
52 type_system_ {std::move(other.type_system_)}
53 {
54 }
55 NO_MOVE_OPERATOR(FullTypeSystem);
56 ~FullTypeSystem() = default;
GetSort(const PandaString & name)57 SortIdx GetSort(const PandaString &name)
58 {
59 return sort_[name];
60 }
ImageOfType(const Type & type)61 const PandaString &ImageOfType(const Type &type)
62 {
63 return type_image_[type];
64 }
ImageOfTypeParams(const TypeParams & type_params)65 PandaString ImageOfTypeParams(const TypeParams &type_params)
66 {
67 return type_image_.ImageOfTypeParams(type_params);
68 }
GetTypeSystem()69 TypeSystem &GetTypeSystem()
70 {
71 return type_system_;
72 }
73
74 private:
75 SortNames sort_;
76 TypeImage type_image_;
77 SortIdx bot_sort_;
78 SortIdx top_sort_;
79 TypeSystem type_system_;
80 };
81
82 struct TypeSystems::Impl {
Implpanda::verifier::TypeSystems::Impl83 explicit Impl(size_t numThreads) : type_systems {}
84 {
85 for (auto kind : {TypeSystemKind::PANDA, TypeSystemKind::JAVA}) {
86 type_systems[kind].reserve(numThreads);
87 for (ThreadNum threadNum = 0; threadNum < numThreads; threadNum++) {
88 type_systems[kind].emplace_back(kind, threadNum);
89 }
90 }
91 }
92
GetFullTypeSystempanda::verifier::TypeSystems::Impl93 FullTypeSystem &GetFullTypeSystem(TypeSystem *tsys)
94 {
95 return type_systems[tsys->GetKind()][tsys->GetThreadNum()];
96 }
97
GetFullTypeSystempanda::verifier::TypeSystems::Impl98 FullTypeSystem &GetFullTypeSystem(TypeSystemKind kind, ThreadNum threadNum)
99 {
100 return type_systems[kind][threadNum];
101 }
102
103 private:
104 EnumArray<PandaVector<FullTypeSystem>, TypeSystemKind, TypeSystemKind::PANDA, TypeSystemKind::JAVA> type_systems;
105 };
106
Initialize(size_t numThreads)107 void TypeSystems::Initialize(size_t numThreads)
108 {
109 if (impl != nullptr) {
110 return;
111 }
112 impl = new (mem::AllocatorAdapter<TypeSystems::Impl>().allocate(1)) Impl {numThreads};
113 ASSERT(impl != nullptr);
114 }
115
Destroy()116 void TypeSystems::Destroy()
117 {
118 if (impl == nullptr) {
119 return;
120 }
121 impl->~Impl();
122 mem::AllocatorAdapter<TypeSystems::Impl>().deallocate(impl, 1);
123 impl = nullptr;
124 }
125
ImageOfType(const Type & type)126 const PandaString &TypeSystems::ImageOfType(const Type &type)
127 {
128 ASSERT(impl != nullptr);
129 return impl->GetFullTypeSystem(&type.GetTypeSystem()).ImageOfType(type);
130 }
131
ImageOfTypeParams(const TypeParams & type)132 PandaString TypeSystems::ImageOfTypeParams(const TypeParams &type)
133 {
134 ASSERT(impl != nullptr);
135 return impl->GetFullTypeSystem(&type.GetTypeSystem()).ImageOfTypeParams(type);
136 }
137
GetSort(TypeSystemKind kind,ThreadNum threadnum,const PandaString & name)138 SortIdx TypeSystems::GetSort(TypeSystemKind kind, ThreadNum threadnum, const PandaString &name)
139 {
140 ASSERT(impl != nullptr);
141 return impl->GetFullTypeSystem(kind, threadnum).GetSort(name);
142 }
143
Get(TypeSystemKind kind,ThreadNum threadnum)144 TypeSystem &TypeSystems::Get(TypeSystemKind kind, ThreadNum threadnum)
145 {
146 ASSERT(impl != nullptr);
147 return impl->GetFullTypeSystem(kind, threadnum).GetTypeSystem();
148 }
149
150 } // namespace panda::verifier
151