Debian Codegraph @

js/src/ds/PointerAndUint7.h

raw
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sw=2 et tw=80:
3 *
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
6 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7
8 #ifndef gc_PointerAndUint7_h
9 #define gc_PointerAndUint7_h
10
11 #include "mozilla/Assertions.h"
12
13 #include <stdint.h>
14
15 namespace js {
16
17 // A class that can store an address and a 7-bit unsigned integer in 64 bits,
18 // even on a 64-bit target.
19 //
20 // On 64-bit targets, it assumes that all supported target architectures
21 // contain at most 57 significant bits in their addresses, and that the valid
22 // address space is split evenly between addresses increasing from 0--(64)--0
23 // and addresses decreasing from 1--(64)--1.
24 //
25 // The 57-significant-bit constraint comes from Intel's 5-level paging scheme
26 // as introduced in the Ice Lake processor line, circa late 2019; see
27 // https://en.wikipedia.org/wiki/Intel_5-level_paging. Prior to that, Intel
28 // required only 48 significant bits. AArch64 requires 52 significant bits,
29 // as of the ARMv8.2 LVA (Large Virtual Addressing) extension, and so is less
30 // constraining than Intel.
31 //
32 // In any case, NaN-boxing of pointers in JS::Value gives us a pretty hard
33 // requirement that we can store pointers in 47 bits. So that constraint will
34 // break before the 57-bit constraint here breaks. See SMDOC in
35 // js/public/Value.h.
36 //
37 // On 32-bit targets, both components are stored unmodified in the upper and
38 // lower 32-bit chunks of the value, and there are no constraints on the
39 // component values.
40
41 #ifdef JS_64BIT
42
43 // The implementation for 64-bit targets.
44 class PointerAndUint7 final {
45 // The representation is: the lowest 57 bits of the pointer are stored in
46 // the top 57 bits of val_, and the Uint7 is stored in the bottom 7 bits.
47 // Hence recovering the pointer is 7-bit signed shift right of val_, and
48 // recovering the UInt7 is an AND with 127. In both cases, that's a single
49 // machine instruction.
50 uint64_t val_;
51
52 static const uint8_t SHIFT_PTR = 7;
53 static const uint64_t MASK_UINT7 = (uint64_t(1) << SHIFT_PTR) - 1;
54
55 static inline bool isRepresentablePtr(void* ptr) {
56 // We require that the top 7 bits (bits 63:57) are the same as bit 56.
57 // That will be the case iff, when we signedly shift `ptr` right by 56
58 // bits, the value is all zeroes or all ones.
59 int64_t s = int64_t(ptr);
60 // s should be bbbb'bbbb'X--(56)--X, for b = 0 or 1, and X can be anything
61 s >>= (64 - SHIFT_PTR - 1); // 56
62 // s should be 0--(64)--0 or 1--(64)--1
63 uint64_t u = uint64_t(s);
64 // Note, this addition can overflow, intentionally.
65 u += 1;
66 // u should be 0--(64)--0 or 0--(63)--01
67 return u <= uint64_t(1);
68 }
69 static inline bool isRepresentableUint7(uint32_t uint7) {
70 return uint7 <= MASK_UINT7;
71 }
72
73 public:
74 inline PointerAndUint7() : val_(0) {}
75 inline PointerAndUint7(void* ptr, uint32_t uint7)
76 : val_((uint64_t(ptr) << SHIFT_PTR) | (uint64_t(uint7 & MASK_UINT7))) {
77 MOZ_ASSERT(isRepresentablePtr(ptr));
78 MOZ_ASSERT(isRepresentableUint7(uint7));
79 }
80 inline void* pointer() const { return (void*)(int64_t(val_) >> SHIFT_PTR); }
81 inline uint32_t uint7() const { return uint32_t(val_ & MASK_UINT7); }
82 };
83
84 static_assert(sizeof(void*) == 8);
85 // "int64_t really is signed"
86 static_assert(((int64_t(1) << 63) >> 63) == int64_t(0xFFFFFFFFFFFFFFFFULL));
87
88 #else
89
90 // The implementation for 32-bit targets.
91 class PointerAndUint7 final {
92 // The representation places the pointer in the upper 32 bits of val_ and
93 // the Uint7 in the lower 32 bits. This is represented using a single
94 // 64-bit field in the hope of increasing the chance that the class will be
95 // passed around in a register-pair rather than through memory.
96 uint64_t val_;
97
98 static const uint8_t SHIFT_PTR = 32;
99 static const uint64_t MASK_UINT7 = (uint64_t(1) << 7) - 1;
100
101 static inline bool isRepresentableUint7(uint32_t uint7) {
102 return uint7 <= MASK_UINT7;
103 }
104
105 public:
106 inline PointerAndUint7() : val_(0) {}
107 inline PointerAndUint7(void* ptr, uint32_t uint7)
108 : val_((uint64_t(uint32_t(ptr)) << SHIFT_PTR) |
109 (uint64_t(uint7) & MASK_UINT7)) {
110 MOZ_ASSERT(isRepresentableUint7(uint7));
111 }
112 inline void* pointer() const { return (void*)(int32_t(val_ >> SHIFT_PTR)); }
113 inline uint32_t uint7() const { return uint32_t(val_ & MASK_UINT7); }
114 };
115
116 static_assert(sizeof(void*) == 4);
117
118 #endif // JS_64BIT
119
120 // We require this for both 32- and 64-bit targets.
121 static_assert(sizeof(PointerAndUint7) == 8);
122
123 } // namespace js
124
125 #endif // gc_PointerAndUint7_h