1 // Copyright 2019 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_OBJECTS_COMPRESSED_SLOTS_H_ 6 #define V8_OBJECTS_COMPRESSED_SLOTS_H_ 7 8 #ifdef V8_COMPRESS_POINTERS 9 10 #include "src/objects/slots.h" 11 12 namespace v8 { 13 namespace internal { 14 15 // A CompressedObjectSlot instance describes a kTaggedSize-sized field ("slot") 16 // holding a compressed tagged pointer (smi or heap object). 17 // Its address() is the address of the slot. 18 // The slot's contents can be read and written using operator* and store(). 19 class CompressedObjectSlot : public SlotBase<CompressedObjectSlot, Tagged_t> { 20 public: 21 using TObject = Object; 22 using THeapObjectSlot = CompressedHeapObjectSlot; 23 24 static constexpr bool kCanBeWeak = false; 25 CompressedObjectSlot()26 CompressedObjectSlot() : SlotBase(kNullAddress) {} CompressedObjectSlot(Address ptr)27 explicit CompressedObjectSlot(Address ptr) : SlotBase(ptr) {} CompressedObjectSlot(Address * ptr)28 explicit CompressedObjectSlot(Address* ptr) 29 : SlotBase(reinterpret_cast<Address>(ptr)) {} 30 inline explicit CompressedObjectSlot(Object* object); CompressedObjectSlot(Object const * const * ptr)31 explicit CompressedObjectSlot(Object const* const* ptr) 32 : SlotBase(reinterpret_cast<Address>(ptr)) {} 33 template <typename T> CompressedObjectSlot(SlotBase<T,TData,kSlotDataAlignment> slot)34 explicit CompressedObjectSlot(SlotBase<T, TData, kSlotDataAlignment> slot) 35 : SlotBase(slot.address()) {} 36 37 // Compares memory representation of a value stored in the slot with given 38 // raw value without decompression. 39 inline bool contains_value(Address raw_value) const; 40 41 // TODO(leszeks): Consider deprecating the operator* load, and always pass the 42 // Isolate. 43 inline Object operator*() const; 44 inline Object load(IsolateRoot isolate) const; 45 inline void store(Object value) const; 46 47 inline Object Acquire_Load() const; 48 inline Object Relaxed_Load() const; 49 inline Object Relaxed_Load(IsolateRoot isolate) const; 50 inline void Relaxed_Store(Object value) const; 51 inline void Release_Store(Object value) const; 52 inline Object Release_CompareAndSwap(Object old, Object target) const; 53 }; 54 55 // A CompressedMaybeObjectSlot instance describes a kTaggedSize-sized field 56 // ("slot") holding a possibly-weak compressed tagged pointer 57 // (think: MaybeObject). 58 // Its address() is the address of the slot. 59 // The slot's contents can be read and written using operator* and store(). 60 class CompressedMaybeObjectSlot 61 : public SlotBase<CompressedMaybeObjectSlot, Tagged_t> { 62 public: 63 using TObject = MaybeObject; 64 using THeapObjectSlot = CompressedHeapObjectSlot; 65 66 static constexpr bool kCanBeWeak = true; 67 CompressedMaybeObjectSlot()68 CompressedMaybeObjectSlot() : SlotBase(kNullAddress) {} CompressedMaybeObjectSlot(Address ptr)69 explicit CompressedMaybeObjectSlot(Address ptr) : SlotBase(ptr) {} CompressedMaybeObjectSlot(Object * ptr)70 explicit CompressedMaybeObjectSlot(Object* ptr) 71 : SlotBase(reinterpret_cast<Address>(ptr)) {} CompressedMaybeObjectSlot(MaybeObject * ptr)72 explicit CompressedMaybeObjectSlot(MaybeObject* ptr) 73 : SlotBase(reinterpret_cast<Address>(ptr)) {} 74 template <typename T> CompressedMaybeObjectSlot(SlotBase<T,TData,kSlotDataAlignment> slot)75 explicit CompressedMaybeObjectSlot( 76 SlotBase<T, TData, kSlotDataAlignment> slot) 77 : SlotBase(slot.address()) {} 78 79 inline MaybeObject operator*() const; 80 inline MaybeObject load(IsolateRoot isolate) const; 81 inline void store(MaybeObject value) const; 82 83 inline MaybeObject Relaxed_Load() const; 84 inline MaybeObject Relaxed_Load(IsolateRoot isolate) const; 85 inline void Relaxed_Store(MaybeObject value) const; 86 inline void Release_CompareAndSwap(MaybeObject old, MaybeObject target) const; 87 }; 88 89 // A CompressedHeapObjectSlot instance describes a kTaggedSize-sized field 90 // ("slot") holding a weak or strong compressed pointer to a heap object (think: 91 // HeapObjectReference). 92 // Its address() is the address of the slot. 93 // The slot's contents can be read and written using operator* and store(). 94 // In case it is known that that slot contains a strong heap object pointer, 95 // ToHeapObject() can be used to retrieve that heap object. 96 class CompressedHeapObjectSlot 97 : public SlotBase<CompressedHeapObjectSlot, Tagged_t> { 98 public: CompressedHeapObjectSlot()99 CompressedHeapObjectSlot() : SlotBase(kNullAddress) {} CompressedHeapObjectSlot(Address ptr)100 explicit CompressedHeapObjectSlot(Address ptr) : SlotBase(ptr) {} CompressedHeapObjectSlot(Object * ptr)101 explicit CompressedHeapObjectSlot(Object* ptr) 102 : SlotBase(reinterpret_cast<Address>(ptr)) {} 103 template <typename T> CompressedHeapObjectSlot(SlotBase<T,TData,kSlotDataAlignment> slot)104 explicit CompressedHeapObjectSlot(SlotBase<T, TData, kSlotDataAlignment> slot) 105 : SlotBase(slot.address()) {} 106 107 inline HeapObjectReference operator*() const; 108 inline HeapObjectReference load(IsolateRoot isolate) const; 109 inline void store(HeapObjectReference value) const; 110 111 inline HeapObject ToHeapObject() const; 112 113 inline void StoreHeapObject(HeapObject value) const; 114 }; 115 116 // An OffHeapCompressedObjectSlot instance describes a kTaggedSize-sized field 117 // ("slot") holding a compressed tagged pointer (smi or heap object). 118 // Unlike CompressedObjectSlot, it does not assume that the slot is on the heap, 119 // and so does not provide an operator* with implicit Isolate* calculation. 120 // Its address() is the address of the slot. 121 // The slot's contents can be read and written using load() and store(). 122 class OffHeapCompressedObjectSlot 123 : public SlotBase<OffHeapCompressedObjectSlot, Tagged_t> { 124 public: 125 using TObject = Object; 126 using THeapObjectSlot = OffHeapCompressedObjectSlot; 127 128 static constexpr bool kCanBeWeak = false; 129 OffHeapCompressedObjectSlot()130 OffHeapCompressedObjectSlot() : SlotBase(kNullAddress) {} OffHeapCompressedObjectSlot(const uint32_t * ptr)131 explicit OffHeapCompressedObjectSlot(const uint32_t* ptr) 132 : SlotBase(reinterpret_cast<Address>(ptr)) {} 133 134 inline Object load(IsolateRoot isolate) const; 135 inline void store(Object value) const; 136 137 inline Object Relaxed_Load(IsolateRoot isolate) const; 138 inline Object Acquire_Load(IsolateRoot isolate) const; 139 inline void Relaxed_Store(Object value) const; 140 inline void Release_Store(Object value) const; 141 inline void Release_CompareAndSwap(Object old, Object target) const; 142 }; 143 144 } // namespace internal 145 } // namespace v8 146 147 #endif // V8_COMPRESS_POINTERS 148 149 #endif // V8_OBJECTS_COMPRESSED_SLOTS_H_ 150