1 /**
2 * Copyright (c) 2021-2024 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 PANDA_RUNTIME_INCLUDE_CORETYPES_TAGGED_VALUE_H_
17 #define PANDA_RUNTIME_INCLUDE_CORETYPES_TAGGED_VALUE_H_
18
19 #include <climits>
20 #include <cstddef>
21 #include "macros.h"
22 #include "libpandabase/mem/mem.h"
23 #include "libpandabase/utils/type_helpers.h"
24 #include "utils/bit_utils.h"
25
26 namespace ark {
27 class ObjectHeader;
28 } // namespace ark
29
30 namespace ark::coretypes {
31
32 // Helper defines for double
33 static constexpr int DOUBLE_MAX_PRECISION = 17;
34 static constexpr int DOUBLE_EXPONENT_BIAS = 0x3FF;
35 static constexpr size_t DOUBLE_SIGNIFICAND_SIZE = 52;
36 static constexpr uint64_t DOUBLE_SIGN_MASK = 0x8000000000000000ULL;
37 static constexpr size_t DOUBLE_EXPONENT_SIZE = 11;
38 static constexpr int DOUBLE_EXPONENT_MAX = 0x7FFULL;
39 static constexpr uint64_t DOUBLE_EXPONENT_MASK = 0x7FFULL << DOUBLE_SIGNIFICAND_SIZE;
40 static constexpr uint64_t DOUBLE_SIGNIFICAND_MASK = 0x000FFFFFFFFFFFFFULL;
41 static constexpr uint64_t DOUBLE_HIDDEN_BIT = 1ULL << DOUBLE_SIGNIFICAND_SIZE;
42
43 static constexpr size_t INT64_BITS = 64;
44 static constexpr size_t INT32_BITS = 32;
45 static constexpr size_t INT16_BITS = 16;
46 static constexpr size_t INT8_BITS = 8;
47
48 // Every double with all of its exponent bits set and its highest mantissa bit set is a quiet NaN.
49 // That leaves 51 bits unaccounted for. We’ll avoid one of those so that we don’t step on Intel’s
50 // “QNaN Floating-Point Indefinite” value, leaving us 50 bits. Those remaining bits can be anything.
51 // so we use a special quietNaN as TaggedInt tag(highest 16bits as 0xFFFF), and need to encode double
52 // to the value will begin with a 16-bit pattern within the range 0x0001..0xFFFE.
53
54 // Nan-boxing pointer is used and the first four bytes are used as tag:
55 // Object: [0x0000] [48 bit direct pointer]
56 // WeakRef: [0x0000] [47 bits direct pointer] | 1 bit 1
57 // / [0x0001] [48 bit any value]
58 // TaggedDouble: ......
59 // \ [0xFFFE] [48 bit any value]
60 // TaggedInt: [0xFFFF] [0x0000] [32 bit signed integer]
61 //
62 // There are some special markers of Object:
63 // False: [56 bits 0] | 0x06 // 0110
64 // True: [56 bits 0] | 0x07 // 0111
65 // Undefined: [56 bits 0] | 0x0a // 1010
66 // Null: [56 bits 0] | 0x02 // 0010
67 // Hole: [56 bits 0] | 0x00 // 0000
68
69 using TaggedType = uint64_t;
70
71 static const TaggedType NULL_POINTER = 0;
72
ReinterpretDoubleToTaggedType(double value)73 inline TaggedType ReinterpretDoubleToTaggedType(double value)
74 {
75 return bit_cast<TaggedType>(value);
76 }
ReinterpretTaggedTypeToDouble(TaggedType value)77 inline double ReinterpretTaggedTypeToDouble(TaggedType value)
78 {
79 return bit_cast<double>(value);
80 }
81
82 class TaggedValue {
83 public:
84 static constexpr size_t TAG_BITS_SIZE = 16;
85 static constexpr size_t TAG_BITS_SHIFT = BitNumbers<TaggedType>() - TAG_BITS_SIZE;
86 static_assert((TAG_BITS_SHIFT + TAG_BITS_SIZE) == sizeof(TaggedType) * CHAR_BIT, "Insufficient bits!");
87 static constexpr TaggedType TAG_MASK = ((1ULL << TAG_BITS_SIZE) - 1ULL) << TAG_BITS_SHIFT;
88 static constexpr TaggedType TAG_INT = TAG_MASK;
89 static constexpr TaggedType TAG_OBJECT = 0x0000ULL << TAG_BITS_SHIFT;
90 static constexpr TaggedType OBJECT_MASK = ~TAG_INT;
91
92 static constexpr TaggedType TAG_SPECIAL_MASK = 0xFFULL;
93 static constexpr TaggedType TAG_SPECIAL_VALUE = 0x02ULL;
94 static constexpr TaggedType TAG_BOOLEAN = 0x04ULL;
95 static constexpr TaggedType TAG_UNDEFINED = 0x08ULL;
96 static constexpr TaggedType TAG_EXCEPTION = 0x10ULL;
97 static constexpr TaggedType TAG_WEAK_FILTER = 0x03ULL;
98 static constexpr TaggedType VALUE_HOLE = TAG_OBJECT | 0x00ULL;
99 static constexpr TaggedType TAG_WEAK_MASK = TAG_OBJECT | 0x01ULL;
100 static constexpr TaggedType VALUE_NULL = TAG_OBJECT | TAG_SPECIAL_VALUE;
101 static constexpr TaggedType VALUE_FALSE =
102 TAG_OBJECT | TAG_BOOLEAN | TAG_SPECIAL_VALUE | static_cast<TaggedType>(false);
103 static constexpr TaggedType VALUE_TRUE =
104 TAG_OBJECT | TAG_BOOLEAN | TAG_SPECIAL_VALUE | static_cast<TaggedType>(true);
105 static constexpr TaggedType VALUE_ZERO = TAG_INT | 0x00ULL;
106 static constexpr TaggedType VALUE_UNDEFINED = TAG_OBJECT | TAG_SPECIAL_VALUE | TAG_UNDEFINED;
107 static constexpr TaggedType VALUE_EXCEPTION = TAG_OBJECT | TAG_SPECIAL_VALUE | TAG_EXCEPTION;
108
109 static constexpr size_t DOUBLE_ENCODE_OFFSET_BIT = 48;
110 static constexpr TaggedType DOUBLE_ENCODE_OFFSET = 1ULL << DOUBLE_ENCODE_OFFSET_BIT;
111
112 static constexpr double VALUE_INFINITY = std::numeric_limits<double>::infinity();
113 static constexpr double VALUE_NAN = std::numeric_limits<double>::quiet_NaN();
114
115 TaggedValue(void *) = delete;
116
TaggedValue()117 constexpr TaggedValue() : value_(NULL_POINTER) {}
118
TaggedValue(TaggedType v)119 constexpr explicit TaggedValue(TaggedType v) : value_(v) {}
120
TaggedValue(int v)121 constexpr explicit TaggedValue(int v) : value_((static_cast<TaggedType>(ark::helpers::ToUnsigned(v))) | TAG_INT) {}
122
TaggedValue(unsigned int v)123 explicit TaggedValue(unsigned int v)
124 {
125 if (static_cast<int32_t>(v) < 0) {
126 value_ = TaggedValue(static_cast<double>(v)).GetRawData();
127 return;
128 }
129 value_ = TaggedValue(static_cast<int32_t>(v)).GetRawData();
130 }
131
GetIntTaggedValue(uint64_t v)132 static uint64_t GetIntTaggedValue(uint64_t v)
133 {
134 ASSERT(INT32_MIN <= static_cast<int32_t>(bit_cast<int64_t>(v)));
135 ASSERT(static_cast<int32_t>(bit_cast<int64_t>(v)) <= INT32_MAX);
136 return static_cast<uint32_t>(v) | TAG_INT;
137 }
138
GetDoubleTaggedValue(uint64_t v)139 static uint64_t GetDoubleTaggedValue(uint64_t v)
140 {
141 return v + DOUBLE_ENCODE_OFFSET;
142 }
143
GetBoolTaggedValue(uint64_t v)144 static uint64_t GetBoolTaggedValue(uint64_t v)
145 {
146 ASSERT(v == 0 || v == 1);
147 return (v == 0) ? static_cast<uint64_t>(coretypes::TaggedValue::False().GetRawData())
148 : static_cast<uint64_t>(coretypes::TaggedValue::True().GetRawData());
149 }
150
GetObjectTaggedValue(uint64_t v)151 static uint64_t GetObjectTaggedValue(uint64_t v)
152 {
153 ASSERT(static_cast<uint32_t>(v) == v);
154 return v;
155 }
156
TaggedValue(int64_t v)157 explicit TaggedValue(int64_t v)
158 {
159 if (UNLIKELY(static_cast<int32_t>(v) != v)) {
160 value_ = TaggedValue(static_cast<double>(v)).GetRawData();
161 return;
162 }
163 value_ = TaggedValue(static_cast<int32_t>(v)).GetRawData();
164 }
165
TaggedValue(bool v)166 constexpr explicit TaggedValue(bool v)
167 : value_(static_cast<TaggedType>(v) | TAG_OBJECT | TAG_BOOLEAN | TAG_SPECIAL_VALUE)
168 {
169 }
170
TaggedValue(double v)171 explicit TaggedValue(double v)
172 {
173 ASSERT_PRINT(!IsImpureNaN(v), "pureNaN will break the encoding of tagged double: "
174 << std::hex << ReinterpretDoubleToTaggedType(v));
175 value_ = ReinterpretDoubleToTaggedType(v) + DOUBLE_ENCODE_OFFSET;
176 }
177
TaggedValue(ObjectHeader * v)178 explicit TaggedValue(ObjectHeader *v) : value_(static_cast<TaggedType>(ToUintPtr(v))) {}
179
TaggedValue(const ObjectHeader * v)180 explicit TaggedValue(const ObjectHeader *v) : value_(static_cast<TaggedType>(ToUintPtr(v))) {}
181
CreateWeakRef()182 inline void CreateWeakRef()
183 {
184 ASSERT_PRINT(IsHeapObject() && ((value_ & TAG_WEAK_FILTER) == 0U),
185 "The least significant two bits of TaggedValue are not zero.");
186 value_ = value_ | TAG_WEAK_MASK;
187 }
188
RemoveWeakTag()189 inline void RemoveWeakTag()
190 {
191 ASSERT_PRINT(IsHeapObject() && ((value_ & TAG_WEAK_MASK) == 1U), "The tagged value is not a weak ref.");
192 value_ = value_ & (~TAG_WEAK_FILTER);
193 }
194
CreateAndGetWeakRef()195 inline TaggedValue CreateAndGetWeakRef()
196 {
197 ASSERT_PRINT(IsHeapObject() && ((value_ & TAG_WEAK_FILTER) == 0U),
198 "The least significant two bits of TaggedValue are not zero.");
199 return TaggedValue(value_ | TAG_WEAK_MASK);
200 }
201
IsWeak()202 inline bool IsWeak() const
203 {
204 return IsHeapObject() && ((value_ & TAG_WEAK_MASK) == 1U);
205 }
206
IsDouble()207 inline bool IsDouble() const
208 {
209 return !IsInt() && !IsObject();
210 }
211
IsInt()212 inline bool IsInt() const
213 {
214 return (value_ & TAG_MASK) == TAG_INT;
215 }
216
IsSpecial()217 inline bool IsSpecial() const
218 {
219 return ((value_ & (~TAG_SPECIAL_MASK)) == 0U) && (((value_ & TAG_SPECIAL_VALUE) != 0U) || IsHole());
220 }
221
IsObject()222 inline bool IsObject() const
223 {
224 return ((value_ & TAG_MASK) == TAG_OBJECT);
225 }
226
IsHeapObject()227 inline bool IsHeapObject() const
228 {
229 return IsObject() && !IsSpecial();
230 }
231
IsNumber()232 inline bool IsNumber() const
233 {
234 return !IsObject();
235 }
236
IsBoolean()237 inline bool IsBoolean() const
238 {
239 return value_ == VALUE_FALSE || value_ == VALUE_TRUE;
240 }
241
GetDouble()242 inline double GetDouble() const
243 {
244 ASSERT_PRINT(IsDouble(), "can not convert TaggedValue to Double : " << std::hex << value_);
245 return ReinterpretTaggedTypeToDouble(value_ - DOUBLE_ENCODE_OFFSET);
246 }
247
GetInt()248 inline int GetInt() const
249 {
250 ASSERT_PRINT(IsInt(), "can not convert TaggedValue to Int :" << std::hex << value_);
251 return static_cast<int>(static_cast<uint32_t>(value_));
252 }
253
GetRawData()254 inline constexpr TaggedType GetRawData() const
255 {
256 return value_;
257 }
258
GetHeapObject()259 inline ObjectHeader *GetHeapObject() const
260 {
261 ASSERT_PRINT(IsHeapObject(), "can not convert TaggedValue to HeapObject :" << std::hex << value_);
262 // NOTE(vpukhov): weakref ignored
263 return reinterpret_cast<ObjectHeader *>(value_ & (~TAG_WEAK_MASK));
264 }
265
266 // This function returns the heap object pointer which may have the weak tag.
GetRawHeapObject()267 inline ObjectHeader *GetRawHeapObject() const
268 {
269 ASSERT_PRINT(IsHeapObject(), "can not convert TaggedValue to HeapObject :" << std::hex << value_);
270 return reinterpret_cast<ObjectHeader *>(value_);
271 }
272
GetWeakReferent()273 inline ObjectHeader *GetWeakReferent() const
274 {
275 ASSERT_PRINT(IsWeak(), "can not convert TaggedValue to WeakRef HeapObject :" << std::hex << value_);
276 return reinterpret_cast<ObjectHeader *>(value_ & (~TAG_WEAK_MASK));
277 }
278
Cast(void * ptr)279 static inline TaggedType Cast(void *ptr)
280 {
281 ASSERT_PRINT(sizeof(void *) == TaggedTypeSize(), "32bit platform is not support yet");
282 return static_cast<TaggedType>(ToUintPtr(ptr));
283 }
284
IsFalse()285 inline bool IsFalse() const
286 {
287 return value_ == VALUE_FALSE;
288 }
289
IsTrue()290 inline bool IsTrue() const
291 {
292 return value_ == VALUE_TRUE;
293 }
294
IsUndefined()295 inline bool IsUndefined() const
296 {
297 return value_ == VALUE_UNDEFINED;
298 }
299
IsNull()300 inline bool IsNull() const
301 {
302 return value_ == VALUE_NULL;
303 }
304
IsUndefinedOrNull()305 inline bool IsUndefinedOrNull() const
306 {
307 return IsNull() || IsUndefined();
308 }
309
IsHole()310 inline bool IsHole() const
311 {
312 return value_ == VALUE_HOLE;
313 }
314
IsException()315 inline bool IsException() const
316 {
317 return value_ == VALUE_EXCEPTION;
318 }
319
False()320 static inline constexpr TaggedValue False()
321 {
322 return TaggedValue(VALUE_FALSE);
323 }
324
True()325 static inline constexpr TaggedValue True()
326 {
327 return TaggedValue(VALUE_TRUE);
328 }
329
Undefined()330 static inline constexpr TaggedValue Undefined()
331 {
332 return TaggedValue(VALUE_UNDEFINED);
333 }
334
Null()335 static inline constexpr TaggedValue Null()
336 {
337 return TaggedValue(VALUE_NULL);
338 }
339
Hole()340 static inline constexpr TaggedValue Hole()
341 {
342 return TaggedValue(VALUE_HOLE);
343 }
344
Exception()345 static inline constexpr TaggedValue Exception()
346 {
347 return TaggedValue(VALUE_EXCEPTION);
348 }
349
TaggedTypeSize()350 static inline constexpr size_t TaggedTypeSize()
351 {
352 return sizeof(TaggedType);
353 }
354
IsImpureNaN(double value)355 static inline bool IsImpureNaN(double value)
356 {
357 // Tests if the double value would break tagged double encoding.
358 return bit_cast<TaggedType>(value) >= (TAG_INT - DOUBLE_ENCODE_OFFSET);
359 }
360
361 inline bool operator==(const TaggedValue &other) const
362 {
363 return value_ == other.value_;
364 }
365
366 inline bool operator!=(const TaggedValue &other) const
367 {
368 return value_ != other.value_;
369 }
370
PackPrimitiveData(uint64_t v)371 static inline TaggedType PackPrimitiveData(uint64_t v)
372 {
373 ASSERT((v & TAG_MASK) == 0);
374 return static_cast<TaggedType>(v | TAG_INT);
375 }
376
UnpackPrimitiveData(TaggedType v)377 static inline uint64_t UnpackPrimitiveData(TaggedType v)
378 {
379 ASSERT((v & TAG_MASK) == TAG_INT);
380 return static_cast<uint64_t>(v & ~TAG_MASK);
381 }
382
383 ~TaggedValue() = default;
384
385 DEFAULT_COPY_SEMANTIC(TaggedValue);
386 DEFAULT_MOVE_SEMANTIC(TaggedValue);
387
388 private:
389 TaggedType value_;
390 };
391
392 // CC-OFFNXT(G.FUD.06) solid logic
JsCastDoubleToInt(double d,size_t bits)393 inline int32_t JsCastDoubleToInt(double d, size_t bits)
394 {
395 int32_t ret = 0;
396 auto u64 = bit_cast<uint64_t>(d);
397 int exp = static_cast<int>((u64 & DOUBLE_EXPONENT_MASK) >> DOUBLE_SIGNIFICAND_SIZE) - DOUBLE_EXPONENT_BIAS;
398 if (exp < static_cast<int>(bits - 1)) {
399 // smaller than INT<bits>_MAX, fast conversion
400 ret = static_cast<int32_t>(d);
401 } else if (exp < static_cast<int>(bits + DOUBLE_SIGNIFICAND_SIZE)) {
402 // Still has significand bits after mod 2^<bits>
403 // Get low <bits> bits by shift left <64 - bits> and shift right <64 - bits>
404 ASSERT(exp > 0);
405 uint64_t value = (((u64 & DOUBLE_SIGNIFICAND_MASK) | DOUBLE_HIDDEN_BIT)
406 << (static_cast<size_t>(exp) - DOUBLE_SIGNIFICAND_SIZE + INT64_BITS - bits)) >>
407 (INT64_BITS - bits);
408 ret = static_cast<int32_t>(value);
409 if ((u64 & DOUBLE_SIGN_MASK) == DOUBLE_SIGN_MASK && ret != INT32_MIN) {
410 ret = -ret;
411 }
412 } else {
413 // No significand bits after mod 2^<bits>, contains NaN and INF
414 ret = 0;
415 }
416 return ret;
417 }
418
419 } // namespace ark::coretypes
420 #endif // PANDA_RUNTIME_INCLUDE_CORETYPES_TAGGED_VALUE_H_
421