ptr-compr-inl.h 3.49 KB
Newer Older
1 2 3 4
// Copyright 2018 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
#ifndef V8_COMMON_PTR_COMPR_INL_H_
#define V8_COMMON_PTR_COMPR_INL_H_
7

8
#include "include/v8-internal.h"
9
#include "src/common/ptr-compr.h"
10
#include "src/execution/isolate.h"
11
#include "src/execution/local-isolate-inl.h"
12 13 14 15

namespace v8 {
namespace internal {

16
#ifdef V8_COMPRESS_POINTERS
17

18
PtrComprCageBase::PtrComprCageBase(const Isolate* isolate)
19
    : address_(isolate->cage_base()) {}
20
PtrComprCageBase::PtrComprCageBase(const LocalIsolate* isolate)
21
    : address_(isolate->cage_base()) {}
22 23

Address PtrComprCageBase::address() const {
24 25
  Address ret = address_;
  ret = reinterpret_cast<Address>(V8_ASSUME_ALIGNED(
26
      reinterpret_cast<void*>(ret), kPtrComprCageBaseAlignment));
27 28 29
  return ret;
}

30 31 32
// Compresses full-pointer representation of a tagged value to on-heap
// representation.
V8_INLINE Tagged_t CompressTagged(Address tagged) {
33
  return static_cast<Tagged_t>(static_cast<uint32_t>(tagged));
34 35
}

36 37 38 39 40 41
V8_INLINE constexpr Address GetPtrComprCageBaseAddress(Address on_heap_addr) {
  return RoundDown<kPtrComprCageBaseAlignment>(on_heap_addr);
}

V8_INLINE Address GetPtrComprCageBaseAddress(PtrComprCageBase cage_base) {
  return cage_base.address();
42 43
}

44 45 46
V8_INLINE constexpr PtrComprCageBase GetPtrComprCageBaseFromOnHeapAddress(
    Address address) {
  return PtrComprCageBase(GetPtrComprCageBaseAddress(address));
47 48 49 50
}

// Decompresses smi value.
V8_INLINE Address DecompressTaggedSigned(Tagged_t raw_value) {
51 52
  // For runtime code the upper 32-bits of the Smi value do not matter.
  return static_cast<Address>(raw_value);
53 54 55 56
}

// Decompresses weak or strong heap object pointer or forwarding pointer,
// preserving both weak- and smi- tags.
57 58
template <typename TOnHeapAddress>
V8_INLINE Address DecompressTaggedPointer(TOnHeapAddress on_heap_addr,
59
                                          Tagged_t raw_value) {
60 61
  return GetPtrComprCageBaseAddress(on_heap_addr) +
         static_cast<Address>(raw_value);
62 63 64
}

// Decompresses any tagged value, preserving both weak- and smi- tags.
65 66
template <typename TOnHeapAddress>
V8_INLINE Address DecompressTaggedAny(TOnHeapAddress on_heap_addr,
67
                                      Tagged_t raw_value) {
68
  return DecompressTaggedPointer(on_heap_addr, raw_value);
69 70
}

71 72 73 74
STATIC_ASSERT(kPtrComprCageReservationSize ==
              Internals::kPtrComprCageReservationSize);
STATIC_ASSERT(kPtrComprCageBaseAlignment ==
              Internals::kPtrComprCageBaseAlignment);
75

76 77 78
#else

V8_INLINE Tagged_t CompressTagged(Address tagged) { UNREACHABLE(); }
79

80 81 82 83
V8_INLINE constexpr PtrComprCageBase GetPtrComprCageBaseFromOnHeapAddress(
    Address address) {
  return PtrComprCageBase();
}
84

85 86 87 88 89 90 91 92 93 94 95 96 97 98
V8_INLINE Address DecompressTaggedSigned(Tagged_t raw_value) { UNREACHABLE(); }

template <typename TOnHeapAddress>
V8_INLINE Address DecompressTaggedPointer(TOnHeapAddress on_heap_addr,
                                          Tagged_t raw_value) {
  UNREACHABLE();
}

template <typename TOnHeapAddress>
V8_INLINE Address DecompressTaggedAny(TOnHeapAddress on_heap_addr,
                                      Tagged_t raw_value) {
  UNREACHABLE();
}

99 100 101 102
V8_INLINE Address GetPtrComprCageBaseAddress(Address on_heap_addr) {
  UNREACHABLE();
}

103
#endif  // V8_COMPRESS_POINTERS
104

105
V8_INLINE PtrComprCageBase GetPtrComprCageBase(HeapObject object) {
106 107 108
  return GetPtrComprCageBaseFromOnHeapAddress(object.ptr());
}

109 110
}  // namespace internal
}  // namespace v8
111

112
#endif  // V8_COMMON_PTR_COMPR_INL_H_