1/*
2 * Copyright (c) 2023 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#ifndef ECMASCRIPT_GLOBAL_INDEX_H
17#define ECMASCRIPT_GLOBAL_INDEX_H
18
19namespace panda::ecmascript {
20class GlobalIndex {
21public:
22    static constexpr uint32_t GLOBAL_CONST_BITFILED_NUM = 10;
23    static constexpr uint32_t GLOBAL_ENV_BITFILED_NUM = 10;
24    static constexpr uint32_t BUILTIN_ENTRIES_BITFILED_NUM = 10;
25    using GlobalConstBits = BitField<size_t, 0, GLOBAL_CONST_BITFILED_NUM>;
26    using GlobalEnvBits = GlobalConstBits::NextField<size_t, GLOBAL_ENV_BITFILED_NUM>;
27    using BuiltinEntriesBits = GlobalEnvBits::NextField<size_t, BUILTIN_ENTRIES_BITFILED_NUM>;
28
29    GlobalIndex() = default;
30    explicit GlobalIndex(uint32_t index) : index_(index) {}
31    explicit GlobalIndex(int32_t index) : index_(static_cast<uint32_t>(index)) {}
32
33    int GetGlobalConstId() const
34    {
35        int id = static_cast<int>(GlobalConstBits::Decode(index_));
36        return id - 1;
37    }
38
39    void UpdateGlobalConstId(size_t id)
40    {
41        index_ = GlobalConstBits::Update(index_, id + 1);
42    }
43
44    bool IsGlobalConstId() const
45    {
46        return GlobalConstBits::Decode(index_);
47    }
48
49    int GetGlobalEnvId() const
50    {
51        int id = static_cast<int>(GlobalEnvBits::Decode(index_));
52        return id - 1;
53    }
54
55    void UpdateGlobalEnvId(size_t id)
56    {
57        index_ = GlobalEnvBits::Update(index_, id + 1);
58    }
59
60    bool IsGlobalEnvId() const
61    {
62        return GlobalEnvBits::Decode(index_);
63    }
64
65    int GetBuiltinEntriesId() const
66    {
67        int id = static_cast<int>(BuiltinEntriesBits::Decode(index_));
68        return id - 1;
69    }
70
71    void UpdateBuiltinEntriesId(size_t id)
72    {
73        index_ = BuiltinEntriesBits::Update(index_, id + 1);
74    }
75
76    bool IsBuiltinEntriesId() const
77    {
78        return BuiltinEntriesBits::Decode(index_);
79    }
80
81    uint32_t GetGlobalIndex() const
82    {
83        return index_;
84    }
85
86    void UpdateGlobalIndex(size_t index)
87    {
88        index_ = index;
89    }
90
91    bool operator!=(const GlobalIndex &right) const
92    {
93        return index_ != right.index_;
94    }
95
96    bool operator==(const GlobalIndex &right) const
97    {
98        return index_ == right.index_;
99    }
100
101private:
102    uint32_t index_ {0};
103};
104}  // namespace panda::ecmascript
105
106#endif  // ECMASCRIPT_GLOBAL_INDEX_MAP_H