• 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 #ifndef ECMASCRIPT_SNAPSHOT_MEM_SLOT_BIT_H
17 #define ECMASCRIPT_SNAPSHOT_MEM_SLOT_BIT_H
18 
19 #include "ecmascript/mem/c_containers.h"
20 #include "ecmascript/mem/slots.h"
21 #include "ecmascript/snapshot/mem/constants.h"
22 
23 #include "utils/bit_field.h"
24 
25 namespace panda::ecmascript {
26 class SlotBit final {
27 public:
28     ~SlotBit() = default;
29     explicit SlotBit() = delete;
30 
31     DEFAULT_COPY_SEMANTIC(SlotBit);
32     DEFAULT_MOVE_SEMANTIC(SlotBit);
33 
SlotBit(uint64_t value)34     explicit SlotBit(uint64_t value) : value_(value) {}
35 
36     using ObjectIndexBits = BitField<uint32_t, 0, OBJECT_INDEX_BIT_NUMBER>;
37     using ObjectToStringBits = ObjectIndexBits::NextField<bool, OBJECT_TO_STRING_FLAG_NUMBER>;
38     using ObjectInConstantsIndexBits = ObjectToStringBits::NextField<uint8_t, OBJECT_IN_CONSTANTS_INDEX_NUMBER>;
39     using ObjectTypeBits = ObjectInConstantsIndexBits::NextField<uint8_t, OBJECT_TYPE_BIT_NUMBER>;
40     using ObjectSizeBits = ObjectTypeBits::NextField<size_t, OBJECT_SIZE_BIT_NUMBER>;
41     using ObjectSpecial = ObjectSizeBits::NextField<bool, OBJECT_SPECIAL>;
42     using IsReferenceSlotBits = ObjectSpecial::NextField<uint32_t, IS_REFERENCE_SLOT_BIT_NUMBER>;
43 
SetReferenceToString(bool flag)44     void SetReferenceToString(bool flag)
45     {
46         ObjectToStringBits::Set<uint64_t>(flag, &value_);
47     }
48 
SetObjectInConstantsIndex(size_t index)49     void SetObjectInConstantsIndex(size_t index)
50     {
51         ObjectInConstantsIndexBits::Set<uint64_t>(index, &value_);
52     }
53 
GetObjectInConstantsIndex()54     size_t GetObjectInConstantsIndex() const
55     {
56         return ObjectInConstantsIndexBits::Decode(value_);
57     }
58 
IsReferenceToString()59     bool IsReferenceToString() const
60     {
61         return ObjectToStringBits::Decode(value_);
62     }
63 
SetObjectType(uint8_t object_type)64     void SetObjectType(uint8_t object_type)
65     {
66         ObjectTypeBits::Set<uint64_t>(object_type, &value_);
67     }
68 
SetObjectSize(size_t object_size)69     void SetObjectSize(size_t object_size)
70     {
71         ObjectSizeBits::Set<uint64_t>(object_size, &value_);
72     }
73 
SetObjectIndex(uint32_t object_index)74     void SetObjectIndex(uint32_t object_index)
75     {
76         ObjectIndexBits::Set<uint64_t>(object_index, &value_);
77     }
78 
GetValue()79     uint64_t GetValue() const
80     {
81         return value_;
82     }
83 
GetObjectIndex()84     uint32_t GetObjectIndex() const
85     {
86         return ObjectIndexBits::Decode(value_);
87     }
88 
GetObjectType()89     uint8_t GetObjectType() const
90     {
91         return ObjectTypeBits::Decode(value_);
92     }
93 
GetObjectSize()94     size_t GetObjectSize() const
95     {
96         return ObjectSizeBits::Decode(value_);
97     }
98 
IsReferenceSlot()99     bool IsReferenceSlot() const
100     {
101         return IsReferenceSlotBits::Decode(value_) == 0;
102     }
103 
IsSpecial()104     bool IsSpecial() const
105     {
106         return ObjectSpecial::Decode(value_);
107     }
108 
SetObjectSpecial()109     void SetObjectSpecial()
110     {
111         ObjectSpecial::Set<uint64_t>(true, &value_);
112     }
113 
ClearObjectSpecialFlag()114     void ClearObjectSpecialFlag()
115     {
116         ObjectSpecial::Set<uint64_t>(false, &value_);
117     }
118 
119 private:
120     uint64_t value_;
121 };
122 
123 class SerializeHelper final {
124 public:
125     static uint8_t GetObjectType(TaggedObject *objectHeader);
126 
127     static SlotBit AddObjectHeaderToData(TaggedObject *objectHeader, CQueue<TaggedObject *> *queue,
128                                          std::unordered_map<uint64_t, ecmascript::SlotBit> *data, size_t index = 0);
129     static void AddTaggedObjectRangeToData(ObjectSlot start, ObjectSlot end, CQueue<TaggedObject *> *queue,
130                                            std::unordered_map<uint64_t, ecmascript::SlotBit> *data);
131 };
132 }  // namespace panda::ecmascript
133 
134 #endif  // ECMASCRIPT_SNAPSHOT_MEM_SLOT_BIT_H
135