1 // Copyright 2018 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef V8_COMMON_PTR_COMPR_INL_H_
6 #define V8_COMMON_PTR_COMPR_INL_H_
7
8 #include "include/v8-internal.h"
9 #include "src/execution/isolate.h"
10 #include "src/execution/local-isolate-inl.h"
11
12 namespace v8 {
13 namespace internal {
14
15 #ifdef V8_COMPRESS_POINTERS
16
PtrComprCageBase(const Isolate * isolate)17 PtrComprCageBase::PtrComprCageBase(const Isolate* isolate)
18 : address_(isolate->cage_base()) {}
PtrComprCageBase(const LocalIsolate * isolate)19 PtrComprCageBase::PtrComprCageBase(const LocalIsolate* isolate)
20 : address_(isolate->cage_base()) {}
21
address()22 Address PtrComprCageBase::address() const {
23 Address ret = address_;
24 ret = reinterpret_cast<Address>(V8_ASSUME_ALIGNED(
25 reinterpret_cast<void*>(ret), kPtrComprCageBaseAlignment));
26 return ret;
27 }
28
29 // Compresses full-pointer representation of a tagged value to on-heap
30 // representation.
CompressTagged(Address tagged)31 V8_INLINE Tagged_t CompressTagged(Address tagged) {
32 return static_cast<Tagged_t>(static_cast<uint32_t>(tagged));
33 }
34
GetPtrComprCageBaseAddress(Address on_heap_addr)35 V8_INLINE constexpr Address GetPtrComprCageBaseAddress(Address on_heap_addr) {
36 return RoundDown<kPtrComprCageBaseAlignment>(on_heap_addr);
37 }
38
GetPtrComprCageBaseAddress(PtrComprCageBase cage_base)39 V8_INLINE Address GetPtrComprCageBaseAddress(PtrComprCageBase cage_base) {
40 return cage_base.address();
41 }
42
GetPtrComprCageBaseFromOnHeapAddress(Address address)43 V8_INLINE constexpr PtrComprCageBase GetPtrComprCageBaseFromOnHeapAddress(
44 Address address) {
45 return PtrComprCageBase(GetPtrComprCageBaseAddress(address));
46 }
47
48 // Decompresses smi value.
DecompressTaggedSigned(Tagged_t raw_value)49 V8_INLINE Address DecompressTaggedSigned(Tagged_t raw_value) {
50 // For runtime code the upper 32-bits of the Smi value do not matter.
51 return static_cast<Address>(raw_value);
52 }
53
54 // Decompresses weak or strong heap object pointer or forwarding pointer,
55 // preserving both weak- and smi- tags.
56 template <typename TOnHeapAddress>
DecompressTaggedPointer(TOnHeapAddress on_heap_addr,Tagged_t raw_value)57 V8_INLINE Address DecompressTaggedPointer(TOnHeapAddress on_heap_addr,
58 Tagged_t raw_value) {
59 return GetPtrComprCageBaseAddress(on_heap_addr) +
60 static_cast<Address>(raw_value);
61 }
62
63 // Decompresses any tagged value, preserving both weak- and smi- tags.
64 template <typename TOnHeapAddress>
DecompressTaggedAny(TOnHeapAddress on_heap_addr,Tagged_t raw_value)65 V8_INLINE Address DecompressTaggedAny(TOnHeapAddress on_heap_addr,
66 Tagged_t raw_value) {
67 return DecompressTaggedPointer(on_heap_addr, raw_value);
68 }
69
70 #else
71
72 V8_INLINE Tagged_t CompressTagged(Address tagged) { UNREACHABLE(); }
73
74 V8_INLINE constexpr PtrComprCageBase GetPtrComprCageBaseFromOnHeapAddress(
75 Address address) {
76 return PtrComprCageBase();
77 }
78
79 V8_INLINE Address DecompressTaggedSigned(Tagged_t raw_value) { UNREACHABLE(); }
80
81 template <typename TOnHeapAddress>
82 V8_INLINE Address DecompressTaggedPointer(TOnHeapAddress on_heap_addr,
83 Tagged_t raw_value) {
84 UNREACHABLE();
85 }
86
87 template <typename TOnHeapAddress>
88 V8_INLINE Address DecompressTaggedAny(TOnHeapAddress on_heap_addr,
89 Tagged_t raw_value) {
90 UNREACHABLE();
91 }
92
93 V8_INLINE Address GetPtrComprCageBaseAddress(Address on_heap_addr) {
94 UNREACHABLE();
95 }
96
97 #endif // V8_COMPRESS_POINTERS
98
GetPtrComprCageBase(HeapObject object)99 V8_INLINE PtrComprCageBase GetPtrComprCageBase(HeapObject object) {
100 return GetPtrComprCageBaseFromOnHeapAddress(object.ptr());
101 }
102
103 } // namespace internal
104 } // namespace v8
105
106 #endif // V8_COMMON_PTR_COMPR_INL_H_
107