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 #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 panda {
27 class ObjectHeader;
28 } // namespace panda
29
30 namespace panda::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>(panda::helpers::ToUnsigned(v))) | TAG_INT)
122 {
123 }
124
TaggedValue(unsigned int v)125 explicit TaggedValue(unsigned int v)
126 {
127 if (static_cast<int32_t>(v) < 0) {
128 value_ = TaggedValue(static_cast<double>(v)).GetRawData();
129 return;
130 }
131 value_ = TaggedValue(static_cast<int32_t>(v)).GetRawData();
132 }
133
GetIntTaggedValue(uint64_t v)134 static uint64_t GetIntTaggedValue(uint64_t v)
135 {
136 ASSERT(INT32_MIN <= static_cast<int32_t>(bit_cast<int64_t>(v)));
137 ASSERT(static_cast<int32_t>(bit_cast<int64_t>(v)) <= INT32_MAX);
138 return static_cast<uint32_t>(v) | TAG_INT;
139 }
140
GetDoubleTaggedValue(uint64_t v)141 static uint64_t GetDoubleTaggedValue(uint64_t v)
142 {
143 return v + DOUBLE_ENCODE_OFFSET;
144 }
145
GetBoolTaggedValue(uint64_t v)146 static uint64_t GetBoolTaggedValue(uint64_t v)
147 {
148 ASSERT(v == 0 || v == 1);
149 return (v == 0) ? static_cast<uint64_t>(coretypes::TaggedValue::False().GetRawData())
150 : static_cast<uint64_t>(coretypes::TaggedValue::True().GetRawData());
151 }
152
GetObjectTaggedValue(uint64_t v)153 static uint64_t GetObjectTaggedValue(uint64_t v)
154 {
155 ASSERT(static_cast<uint32_t>(v) == v);
156 return v;
157 }
158
TaggedValue(int64_t v)159 explicit TaggedValue(int64_t v)
160 {
161 if (UNLIKELY(static_cast<int32_t>(v) != v)) {
162 value_ = TaggedValue(static_cast<double>(v)).GetRawData();
163 return;
164 }
165 value_ = TaggedValue(static_cast<int32_t>(v)).GetRawData();
166 }
167
TaggedValue(bool v)168 constexpr explicit TaggedValue(bool v)
169 : value_(static_cast<TaggedType>(v) | TAG_OBJECT | TAG_BOOLEAN | TAG_SPECIAL_VALUE)
170 {
171 }
172
TaggedValue(double v)173 explicit TaggedValue(double v)
174 {
175 ASSERT_PRINT(!IsImpureNaN(v), "pureNaN will break the encoding of tagged double: "
176 << std::hex << ReinterpretDoubleToTaggedType(v));
177 value_ = ReinterpretDoubleToTaggedType(v) + DOUBLE_ENCODE_OFFSET;
178 }
179
TaggedValue(ObjectHeader * v)180 explicit TaggedValue(ObjectHeader *v) : value_(static_cast<TaggedType>(ToUintPtr(v))) {}
181
TaggedValue(const ObjectHeader * v)182 explicit TaggedValue(const ObjectHeader *v) : value_(static_cast<TaggedType>(ToUintPtr(v))) {}
183
CreateWeakRef()184 inline void CreateWeakRef()
185 {
186 ASSERT_PRINT(IsHeapObject() && ((value_ & TAG_WEAK_FILTER) == 0U),
187 "The least significant two bits of TaggedValue are not zero.");
188 value_ = value_ | TAG_WEAK_MASK;
189 }
190
RemoveWeakTag()191 inline void RemoveWeakTag()
192 {
193 ASSERT_PRINT(IsHeapObject() && ((value_ & TAG_WEAK_MASK) == 1U), "The tagged value is not a weak ref.");
194 value_ = value_ & (~TAG_WEAK_FILTER);
195 }
196
CreateAndGetWeakRef()197 inline TaggedValue CreateAndGetWeakRef()
198 {
199 ASSERT_PRINT(IsHeapObject() && ((value_ & TAG_WEAK_FILTER) == 0U),
200 "The least significant two bits of TaggedValue are not zero.");
201 return TaggedValue(value_ | TAG_WEAK_MASK);
202 }
203
IsWeak()204 inline bool IsWeak() const
205 {
206 return IsHeapObject() && ((value_ & TAG_WEAK_MASK) == 1U);
207 }
208
IsDouble()209 inline bool IsDouble() const
210 {
211 return !IsInt() && !IsObject();
212 }
213
IsInt()214 inline bool IsInt() const
215 {
216 return (value_ & TAG_MASK) == TAG_INT;
217 }
218
IsSpecial()219 inline bool IsSpecial() const
220 {
221 return ((value_ & (~TAG_SPECIAL_MASK)) == 0U) && (((value_ & TAG_SPECIAL_VALUE) != 0U) || IsHole());
222 }
223
IsObject()224 inline bool IsObject() const
225 {
226 return ((value_ & TAG_MASK) == TAG_OBJECT);
227 }
228
IsHeapObject()229 inline bool IsHeapObject() const
230 {
231 return IsObject() && !IsSpecial();
232 }
233
IsNumber()234 inline bool IsNumber() const
235 {
236 return !IsObject();
237 }
238
IsBoolean()239 inline bool IsBoolean() const
240 {
241 return value_ == VALUE_FALSE || value_ == VALUE_TRUE;
242 }
243
GetDouble()244 inline double GetDouble() const
245 {
246 ASSERT_PRINT(IsDouble(), "can not convert TaggedValue to Double : " << std::hex << value_);
247 return ReinterpretTaggedTypeToDouble(value_ - DOUBLE_ENCODE_OFFSET);
248 }
249
GetInt()250 inline int GetInt() const
251 {
252 ASSERT_PRINT(IsInt(), "can not convert TaggedValue to Int :" << std::hex << value_);
253 return static_cast<int>(static_cast<uint32_t>(value_));
254 }
255
GetRawData()256 inline constexpr TaggedType GetRawData() const
257 {
258 return value_;
259 }
260
GetHeapObject()261 inline ObjectHeader *GetHeapObject() const
262 {
263 ASSERT_PRINT(IsHeapObject(), "can not convert TaggedValue to HeapObject :" << std::hex << value_);
264 return reinterpret_cast<ObjectHeader *>(value_ & (~TAG_WEAK_MASK));
265 }
266
267 // This function returns the heap object pointer which may have the weak tag.
GetRawHeapObject()268 inline ObjectHeader *GetRawHeapObject() const
269 {
270 ASSERT_PRINT(IsHeapObject(), "can not convert TaggedValue to HeapObject :" << std::hex << value_);
271 return reinterpret_cast<ObjectHeader *>(value_);
272 }
273
GetWeakReferent()274 inline ObjectHeader *GetWeakReferent() const
275 {
276 ASSERT_PRINT(IsWeak(), "can not convert TaggedValue to WeakRef HeapObject :" << std::hex << value_);
277 return reinterpret_cast<ObjectHeader *>(value_ & (~TAG_WEAK_MASK));
278 }
279
Cast(void * ptr)280 static inline TaggedType Cast(void *ptr)
281 {
282 ASSERT_PRINT(sizeof(void *) == TaggedTypeSize(), "32bit platform is not support yet");
283 return static_cast<TaggedType>(ToUintPtr(ptr));
284 }
285
IsFalse()286 inline bool IsFalse() const
287 {
288 return value_ == VALUE_FALSE;
289 }
290
IsTrue()291 inline bool IsTrue() const
292 {
293 return value_ == VALUE_TRUE;
294 }
295
IsUndefined()296 inline bool IsUndefined() const
297 {
298 return value_ == VALUE_UNDEFINED;
299 }
300
IsNull()301 inline bool IsNull() const
302 {
303 return value_ == VALUE_NULL;
304 }
305
IsUndefinedOrNull()306 inline bool IsUndefinedOrNull() const
307 {
308 return IsNull() || IsUndefined();
309 }
310
IsHole()311 inline bool IsHole() const
312 {
313 return value_ == VALUE_HOLE;
314 }
315
IsException()316 inline bool IsException() const
317 {
318 return value_ == VALUE_EXCEPTION;
319 }
320
False()321 static inline constexpr TaggedValue False()
322 {
323 return TaggedValue(VALUE_FALSE);
324 }
325
True()326 static inline constexpr TaggedValue True()
327 {
328 return TaggedValue(VALUE_TRUE);
329 }
330
Undefined()331 static inline constexpr TaggedValue Undefined()
332 {
333 return TaggedValue(VALUE_UNDEFINED);
334 }
335
Null()336 static inline constexpr TaggedValue Null()
337 {
338 return TaggedValue(VALUE_NULL);
339 }
340
Hole()341 static inline constexpr TaggedValue Hole()
342 {
343 return TaggedValue(VALUE_HOLE);
344 }
345
Exception()346 static inline constexpr TaggedValue Exception()
347 {
348 return TaggedValue(VALUE_EXCEPTION);
349 }
350
TaggedTypeSize()351 static inline constexpr size_t TaggedTypeSize()
352 {
353 return sizeof(TaggedType);
354 }
355
IsImpureNaN(double value)356 static inline bool IsImpureNaN(double value)
357 {
358 // Tests if the double value would break tagged double encoding.
359 return bit_cast<TaggedType>(value) >= (TAG_INT - DOUBLE_ENCODE_OFFSET);
360 }
361
362 inline bool operator==(const TaggedValue &other) const
363 {
364 return value_ == other.value_;
365 }
366
367 inline bool operator!=(const TaggedValue &other) const
368 {
369 return value_ != other.value_;
370 }
371
PackPrimitiveData(uint64_t v)372 static inline TaggedType PackPrimitiveData(uint64_t v)
373 {
374 ASSERT((v & TAG_MASK) == 0);
375 return static_cast<TaggedType>(v | TAG_INT);
376 }
377
UnpackPrimitiveData(TaggedType v)378 static inline uint64_t UnpackPrimitiveData(TaggedType v)
379 {
380 ASSERT((v & TAG_MASK) == TAG_INT);
381 return static_cast<uint64_t>(v & ~TAG_MASK);
382 }
383
384 ~TaggedValue() = default;
385
386 DEFAULT_COPY_SEMANTIC(TaggedValue);
387 DEFAULT_MOVE_SEMANTIC(TaggedValue);
388
389 private:
390 TaggedType value_;
391 };
392
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 uint64_t value = (((u64 & DOUBLE_SIGNIFICAND_MASK) | DOUBLE_HIDDEN_BIT)
405 << (exp - DOUBLE_SIGNIFICAND_SIZE + INT64_BITS - bits)) >>
406 (INT64_BITS - bits);
407 ret = static_cast<int32_t>(value);
408 if ((u64 & DOUBLE_SIGN_MASK) == DOUBLE_SIGN_MASK && ret != INT32_MIN) {
409 ret = -ret;
410 }
411 } else {
412 // No significand bits after mod 2^<bits>, contains NaN and INF
413 ret = 0;
414 }
415 return ret;
416 }
417
418 } // namespace panda::coretypes
419 #endif // PANDA_RUNTIME_INCLUDE_CORETYPES_TAGGED_VALUE_H
420