ptr-compr-inl.h 7.3 KB
Newer Older
1 2 3 4 5 6 7 8 9
// 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.

#ifndef V8_PTR_COMPR_INL_H_
#define V8_PTR_COMPR_INL_H_

#if V8_TARGET_ARCH_64_BIT

10
#include "src/objects/heap-object-inl.h"
11 12 13 14 15 16 17 18
#include "src/ptr-compr.h"

namespace v8 {
namespace internal {

// Compresses full-pointer representation of a tagged value to on-heap
// representation.
V8_INLINE Tagged_t CompressTagged(Address tagged) {
19
  return static_cast<Tagged_t>(static_cast<uint32_t>(tagged));
20 21 22 23 24 25 26 27 28 29
}

// Calculates isolate root value from any on-heap address.
V8_INLINE Address GetRootFromOnHeapAddress(Address addr) {
  return RoundDown(addr + kPtrComprIsolateRootBias,
                   kPtrComprIsolateRootAlignment);
}

// Decompresses weak or strong heap object pointer or forwarding pointer,
// preserving both weak- and smi- tags.
30 31 32 33 34
V8_INLINE Address DecompressTaggedPointer(Address on_heap_addr,
                                          Tagged_t raw_value) {
  static_assert(kTaggedSize == kSystemPointerSize, "has to be updated");
  static_assert(!std::is_same<int32_t, Tagged_t>::value, "remove cast below");
  int32_t value = static_cast<int32_t>(raw_value);
35 36 37 38 39 40 41
  Address root = GetRootFromOnHeapAddress(on_heap_addr);
  // Current compression scheme requires value to be sign-extended to inptr_t
  // before adding the |root|.
  return root + static_cast<Address>(static_cast<intptr_t>(value));
}

// Decompresses any tagged value, preserving both weak- and smi- tags.
42 43 44 45 46
V8_INLINE Address DecompressTaggedAny(Address on_heap_addr,
                                      Tagged_t raw_value) {
  static_assert(kTaggedSize == kSystemPointerSize, "has to be updated");
  static_assert(!std::is_same<int32_t, Tagged_t>::value, "remove cast below");
  int32_t value = static_cast<int32_t>(raw_value);
47 48 49 50 51 52 53 54
  // |root_mask| is 0 if the |value| was a smi or -1 otherwise.
  Address root_mask = -static_cast<Address>(value & kSmiTagMask);
  Address root_or_zero = root_mask & GetRootFromOnHeapAddress(on_heap_addr);
  // Current compression scheme requires value to be sign-extended to inptr_t
  // before adding the |root_or_zero|.
  return root_or_zero + static_cast<Address>(static_cast<intptr_t>(value));
}

55 56 57 58 59
STATIC_ASSERT(kPtrComprHeapReservationSize ==
              Internals::kPtrComprHeapReservationSize);
STATIC_ASSERT(kPtrComprIsolateRootBias == Internals::kPtrComprIsolateRootBias);
STATIC_ASSERT(kPtrComprIsolateRootAlignment ==
              Internals::kPtrComprIsolateRootAlignment);
60 61 62 63 64

//
// CompressedObjectSlot implementation.
//

65
CompressedObjectSlot::CompressedObjectSlot(Object* object)
66 67
    : SlotBase(reinterpret_cast<Address>(&object->ptr_)) {}

68
Object CompressedObjectSlot::operator*() const {
69
  Tagged_t value = *location();
70
  return Object(DecompressTaggedAny(address(), value));
71 72
}

73
void CompressedObjectSlot::store(Object value) const {
74 75 76
  *location() = CompressTagged(value->ptr());
}

77
Object CompressedObjectSlot::Acquire_Load() const {
78
  AtomicTagged_t value = AsAtomicTagged::Acquire_Load(location());
79
  return Object(DecompressTaggedAny(address(), value));
80 81
}

82
Object CompressedObjectSlot::Relaxed_Load() const {
83
  AtomicTagged_t value = AsAtomicTagged::Relaxed_Load(location());
84
  return Object(DecompressTaggedAny(address(), value));
85 86
}

87
void CompressedObjectSlot::Relaxed_Store(Object value) const {
88 89 90 91
  Tagged_t ptr = CompressTagged(value->ptr());
  AsAtomicTagged::Relaxed_Store(location(), ptr);
}

92
void CompressedObjectSlot::Release_Store(Object value) const {
93 94 95 96
  Tagged_t ptr = CompressTagged(value->ptr());
  AsAtomicTagged::Release_Store(location(), ptr);
}

97 98
Object CompressedObjectSlot::Release_CompareAndSwap(Object old,
                                                    Object target) const {
99 100 101 102
  Tagged_t old_ptr = CompressTagged(old->ptr());
  Tagged_t target_ptr = CompressTagged(target->ptr());
  Tagged_t result =
      AsAtomicTagged::Release_CompareAndSwap(location(), old_ptr, target_ptr);
103
  return Object(DecompressTaggedAny(address(), result));
104 105 106 107 108 109 110
}

//
// CompressedMapWordSlot implementation.
//

bool CompressedMapWordSlot::contains_value(Address raw_value) const {
111 112 113
  AtomicTagged_t value = AsAtomicTagged::Relaxed_Load(location());
  return static_cast<uint32_t>(value) ==
         static_cast<uint32_t>(static_cast<Tagged_t>(raw_value));
114 115
}

116
Object CompressedMapWordSlot::operator*() const {
117
  Tagged_t value = *location();
118
  return Object(DecompressTaggedPointer(address(), value));
119 120
}

121
void CompressedMapWordSlot::store(Object value) const {
122 123 124
  *location() = CompressTagged(value.ptr());
}

125
Object CompressedMapWordSlot::Relaxed_Load() const {
126
  AtomicTagged_t value = AsAtomicTagged::Relaxed_Load(location());
127
  return Object(DecompressTaggedPointer(address(), value));
128 129
}

130
void CompressedMapWordSlot::Relaxed_Store(Object value) const {
131 132 133 134
  Tagged_t ptr = CompressTagged(value.ptr());
  AsAtomicTagged::Relaxed_Store(location(), ptr);
}

135
Object CompressedMapWordSlot::Acquire_Load() const {
136
  AtomicTagged_t value = AsAtomicTagged::Acquire_Load(location());
137
  return Object(DecompressTaggedPointer(address(), value));
138 139
}

140
void CompressedMapWordSlot::Release_Store(Object value) const {
141 142 143 144
  Tagged_t ptr = CompressTagged(value->ptr());
  AsAtomicTagged::Release_Store(location(), ptr);
}

145 146
Object CompressedMapWordSlot::Release_CompareAndSwap(Object old,
                                                     Object target) const {
147 148 149 150
  Tagged_t old_ptr = CompressTagged(old->ptr());
  Tagged_t target_ptr = CompressTagged(target->ptr());
  Tagged_t result =
      AsAtomicTagged::Release_CompareAndSwap(location(), old_ptr, target_ptr);
151
  return Object(DecompressTaggedPointer(address(), result));
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
}

//
// CompressedMaybeObjectSlot implementation.
//

MaybeObject CompressedMaybeObjectSlot::operator*() const {
  Tagged_t value = *location();
  return MaybeObject(DecompressTaggedAny(address(), value));
}

void CompressedMaybeObjectSlot::store(MaybeObject value) const {
  *location() = CompressTagged(value->ptr());
}

MaybeObject CompressedMaybeObjectSlot::Relaxed_Load() const {
  AtomicTagged_t value = AsAtomicTagged::Relaxed_Load(location());
  return MaybeObject(DecompressTaggedAny(address(), value));
}

void CompressedMaybeObjectSlot::Relaxed_Store(MaybeObject value) const {
  Tagged_t ptr = CompressTagged(value->ptr());
  AsAtomicTagged::Relaxed_Store(location(), ptr);
}

void CompressedMaybeObjectSlot::Release_CompareAndSwap(
    MaybeObject old, MaybeObject target) const {
  Tagged_t old_ptr = CompressTagged(old->ptr());
  Tagged_t target_ptr = CompressTagged(target->ptr());
  AsAtomicTagged::Release_CompareAndSwap(location(), old_ptr, target_ptr);
}

//
// CompressedHeapObjectSlot implementation.
//

HeapObjectReference CompressedHeapObjectSlot::operator*() const {
  Tagged_t value = *location();
  return HeapObjectReference(DecompressTaggedPointer(address(), value));
}

void CompressedHeapObjectSlot::store(HeapObjectReference value) const {
  *location() = CompressTagged(value.ptr());
}

197
HeapObject CompressedHeapObjectSlot::ToHeapObject() const {
198 199 200
  Tagged_t value = *location();
  DCHECK_EQ(value & kHeapObjectTagMask, kHeapObjectTag);
  return HeapObject::cast(Object(DecompressTaggedPointer(address(), value)));
201 202 203
}

void CompressedHeapObjectSlot::StoreHeapObject(HeapObject value) const {
204
  *location() = CompressTagged(value->ptr());
205 206
}

207 208 209 210 211 212
}  // namespace internal
}  // namespace v8

#endif  // V8_TARGET_ARCH_64_BIT

#endif  // V8_PTR_COMPR_INL_H_