11cb0ef41Sopenharmony_ci// Copyright 2018 the V8 project authors. All rights reserved.
21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
31cb0ef41Sopenharmony_ci// found in the LICENSE file.
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci#ifndef V8_OBJECTS_HEAP_OBJECT_H_
61cb0ef41Sopenharmony_ci#define V8_OBJECTS_HEAP_OBJECT_H_
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci#include "src/common/globals.h"
91cb0ef41Sopenharmony_ci#include "src/objects/instance-type.h"
101cb0ef41Sopenharmony_ci#include "src/objects/objects.h"
111cb0ef41Sopenharmony_ci#include "src/objects/tagged-field.h"
121cb0ef41Sopenharmony_ci#include "src/roots/roots.h"
131cb0ef41Sopenharmony_ci#include "src/torque/runtime-macro-shims.h"
141cb0ef41Sopenharmony_ci#include "src/torque/runtime-support.h"
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ci// Has to be the last include (doesn't have include guards):
171cb0ef41Sopenharmony_ci#include "src/objects/object-macros.h"
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_cinamespace v8 {
201cb0ef41Sopenharmony_cinamespace internal {
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ciclass Heap;
231cb0ef41Sopenharmony_ciclass PrimitiveHeapObject;
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci// HeapObject is the superclass for all classes describing heap allocated
261cb0ef41Sopenharmony_ci// objects.
271cb0ef41Sopenharmony_ciclass HeapObject : public Object {
281cb0ef41Sopenharmony_ci public:
291cb0ef41Sopenharmony_ci  bool is_null() const {
301cb0ef41Sopenharmony_ci    return static_cast<Tagged_t>(ptr()) == static_cast<Tagged_t>(kNullAddress);
311cb0ef41Sopenharmony_ci  }
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci  // [map]: Contains a map which contains the object's reflective
341cb0ef41Sopenharmony_ci  // information.
351cb0ef41Sopenharmony_ci  DECL_GETTER(map, Map)
361cb0ef41Sopenharmony_ci  inline void set_map(Map value);
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci  // This method behaves the same as `set_map` but marks the map transition as
391cb0ef41Sopenharmony_ci  // safe for the concurrent marker (object layout doesn't change) during
401cb0ef41Sopenharmony_ci  // verification.
411cb0ef41Sopenharmony_ci  inline void set_map_safe_transition(Map value);
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ci  inline ObjectSlot map_slot() const;
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ci  // The no-write-barrier version.  This is OK if the object is white and in
461cb0ef41Sopenharmony_ci  // new space, or if the value is an immortal immutable object, like the maps
471cb0ef41Sopenharmony_ci  // of primitive (non-JS) objects like strings, heap numbers etc.
481cb0ef41Sopenharmony_ci  inline void set_map_no_write_barrier(Map value,
491cb0ef41Sopenharmony_ci                                       RelaxedStoreTag = kRelaxedStore);
501cb0ef41Sopenharmony_ci  inline void set_map_no_write_barrier(Map value, ReleaseStoreTag);
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ci  // Access the map using acquire load and release store.
531cb0ef41Sopenharmony_ci  DECL_ACQUIRE_GETTER(map, Map)
541cb0ef41Sopenharmony_ci  inline void set_map(Map value, ReleaseStoreTag);
551cb0ef41Sopenharmony_ci  inline void set_map_safe_transition(Map value, ReleaseStoreTag);
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ci  // Compare-and-swaps map word using release store, returns true if the map
581cb0ef41Sopenharmony_ci  // word was actually swapped.
591cb0ef41Sopenharmony_ci  inline bool release_compare_and_swap_map_word(MapWord old_map_word,
601cb0ef41Sopenharmony_ci                                                MapWord new_map_word);
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ci  // Initialize the map immediately after the object is allocated.
631cb0ef41Sopenharmony_ci  // Do not use this outside Heap.
641cb0ef41Sopenharmony_ci  inline void set_map_after_allocation(
651cb0ef41Sopenharmony_ci      Map value, WriteBarrierMode mode = UPDATE_WRITE_BARRIER);
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_ci  // During garbage collection, the map word of a heap object does not
681cb0ef41Sopenharmony_ci  // necessarily contain a map pointer.
691cb0ef41Sopenharmony_ci  DECL_RELAXED_GETTER(map_word, MapWord)
701cb0ef41Sopenharmony_ci  inline void set_map_word(MapWord map_word, RelaxedStoreTag);
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_ci  // Access the map word using acquire load and release store.
731cb0ef41Sopenharmony_ci  DECL_ACQUIRE_GETTER(map_word, MapWord)
741cb0ef41Sopenharmony_ci  inline void set_map_word(MapWord map_word, ReleaseStoreTag);
751cb0ef41Sopenharmony_ci
761cb0ef41Sopenharmony_ci  // This method exists to help remove GetIsolate/GetHeap from HeapObject, in a
771cb0ef41Sopenharmony_ci  // way that doesn't require passing Isolate/Heap down huge call chains or to
781cb0ef41Sopenharmony_ci  // places where it might not be safe to access it.
791cb0ef41Sopenharmony_ci  inline ReadOnlyRoots GetReadOnlyRoots() const;
801cb0ef41Sopenharmony_ci  // This version is intended to be used for the isolate values produced by
811cb0ef41Sopenharmony_ci  // i::GetPtrComprCageBase(HeapObject) function which may return nullptr.
821cb0ef41Sopenharmony_ci  inline ReadOnlyRoots GetReadOnlyRoots(PtrComprCageBase cage_base) const;
831cb0ef41Sopenharmony_ci
841cb0ef41Sopenharmony_ci  // Whether the object is in the RO heap and the RO heap is shared, or in the
851cb0ef41Sopenharmony_ci  // writable shared heap.
861cb0ef41Sopenharmony_ci  V8_INLINE bool InSharedHeap() const;
871cb0ef41Sopenharmony_ci
881cb0ef41Sopenharmony_ci  V8_INLINE bool InSharedWritableHeap() const;
891cb0ef41Sopenharmony_ci
901cb0ef41Sopenharmony_ci#define IS_TYPE_FUNCTION_DECL(Type) \
911cb0ef41Sopenharmony_ci  V8_INLINE bool Is##Type() const;  \
921cb0ef41Sopenharmony_ci  V8_INLINE bool Is##Type(PtrComprCageBase cage_base) const;
931cb0ef41Sopenharmony_ci  HEAP_OBJECT_TYPE_LIST(IS_TYPE_FUNCTION_DECL)
941cb0ef41Sopenharmony_ci  IS_TYPE_FUNCTION_DECL(HashTableBase)
951cb0ef41Sopenharmony_ci  IS_TYPE_FUNCTION_DECL(SmallOrderedHashTable)
961cb0ef41Sopenharmony_ci  IS_TYPE_FUNCTION_DECL(CodeT)
971cb0ef41Sopenharmony_ci#undef IS_TYPE_FUNCTION_DECL
981cb0ef41Sopenharmony_ci
991cb0ef41Sopenharmony_ci// Oddball checks are faster when they are raw pointer comparisons, so the
1001cb0ef41Sopenharmony_ci// isolate/read-only roots overloads should be preferred where possible.
1011cb0ef41Sopenharmony_ci#define IS_TYPE_FUNCTION_DECL(Type, Value)              \
1021cb0ef41Sopenharmony_ci  V8_INLINE bool Is##Type(Isolate* isolate) const;      \
1031cb0ef41Sopenharmony_ci  V8_INLINE bool Is##Type(LocalIsolate* isolate) const; \
1041cb0ef41Sopenharmony_ci  V8_INLINE bool Is##Type(ReadOnlyRoots roots) const;   \
1051cb0ef41Sopenharmony_ci  V8_INLINE bool Is##Type() const;
1061cb0ef41Sopenharmony_ci  ODDBALL_LIST(IS_TYPE_FUNCTION_DECL)
1071cb0ef41Sopenharmony_ci  IS_TYPE_FUNCTION_DECL(NullOrUndefined, /* unused */)
1081cb0ef41Sopenharmony_ci#undef IS_TYPE_FUNCTION_DECL
1091cb0ef41Sopenharmony_ci
1101cb0ef41Sopenharmony_ci#define DECL_STRUCT_PREDICATE(NAME, Name, name) \
1111cb0ef41Sopenharmony_ci  V8_INLINE bool Is##Name() const;              \
1121cb0ef41Sopenharmony_ci  V8_INLINE bool Is##Name(PtrComprCageBase cage_base) const;
1131cb0ef41Sopenharmony_ci  STRUCT_LIST(DECL_STRUCT_PREDICATE)
1141cb0ef41Sopenharmony_ci#undef DECL_STRUCT_PREDICATE
1151cb0ef41Sopenharmony_ci
1161cb0ef41Sopenharmony_ci  // Converts an address to a HeapObject pointer.
1171cb0ef41Sopenharmony_ci  static inline HeapObject FromAddress(Address address) {
1181cb0ef41Sopenharmony_ci    DCHECK_TAG_ALIGNED(address);
1191cb0ef41Sopenharmony_ci    return HeapObject(address + kHeapObjectTag);
1201cb0ef41Sopenharmony_ci  }
1211cb0ef41Sopenharmony_ci
1221cb0ef41Sopenharmony_ci  // Returns the address of this HeapObject.
1231cb0ef41Sopenharmony_ci  inline Address address() const { return ptr() - kHeapObjectTag; }
1241cb0ef41Sopenharmony_ci
1251cb0ef41Sopenharmony_ci  // Iterates over pointers contained in the object (including the Map).
1261cb0ef41Sopenharmony_ci  // If it's not performance critical iteration use the non-templatized
1271cb0ef41Sopenharmony_ci  // version.
1281cb0ef41Sopenharmony_ci  void Iterate(PtrComprCageBase cage_base, ObjectVisitor* v);
1291cb0ef41Sopenharmony_ci
1301cb0ef41Sopenharmony_ci  template <typename ObjectVisitor>
1311cb0ef41Sopenharmony_ci  inline void IterateFast(PtrComprCageBase cage_base, ObjectVisitor* v);
1321cb0ef41Sopenharmony_ci
1331cb0ef41Sopenharmony_ci  template <typename ObjectVisitor>
1341cb0ef41Sopenharmony_ci  inline void IterateFast(Map map, int object_size, ObjectVisitor* v);
1351cb0ef41Sopenharmony_ci
1361cb0ef41Sopenharmony_ci  // Iterates over all pointers contained in the object except the
1371cb0ef41Sopenharmony_ci  // first map pointer.  The object type is given in the first
1381cb0ef41Sopenharmony_ci  // parameter. This function does not access the map pointer in the
1391cb0ef41Sopenharmony_ci  // object, and so is safe to call while the map pointer is modified.
1401cb0ef41Sopenharmony_ci  // If it's not performance critical iteration use the non-templatized
1411cb0ef41Sopenharmony_ci  // version.
1421cb0ef41Sopenharmony_ci  void IterateBody(PtrComprCageBase cage_base, ObjectVisitor* v);
1431cb0ef41Sopenharmony_ci  void IterateBody(Map map, int object_size, ObjectVisitor* v);
1441cb0ef41Sopenharmony_ci
1451cb0ef41Sopenharmony_ci  template <typename ObjectVisitor>
1461cb0ef41Sopenharmony_ci  inline void IterateBodyFast(PtrComprCageBase cage_base, ObjectVisitor* v);
1471cb0ef41Sopenharmony_ci
1481cb0ef41Sopenharmony_ci  template <typename ObjectVisitor>
1491cb0ef41Sopenharmony_ci  inline void IterateBodyFast(Map map, int object_size, ObjectVisitor* v);
1501cb0ef41Sopenharmony_ci
1511cb0ef41Sopenharmony_ci  // Returns true if the object contains a tagged value at given offset.
1521cb0ef41Sopenharmony_ci  // It is used for invalid slots filtering. If the offset points outside
1531cb0ef41Sopenharmony_ci  // of the object or to the map word, the result is UNDEFINED (!!!).
1541cb0ef41Sopenharmony_ci  V8_EXPORT_PRIVATE bool IsValidSlot(Map map, int offset);
1551cb0ef41Sopenharmony_ci
1561cb0ef41Sopenharmony_ci  // Returns the heap object's size in bytes
1571cb0ef41Sopenharmony_ci  DECL_GETTER(Size, int)
1581cb0ef41Sopenharmony_ci
1591cb0ef41Sopenharmony_ci  // Given a heap object's map pointer, returns the heap size in bytes
1601cb0ef41Sopenharmony_ci  // Useful when the map pointer field is used for other purposes.
1611cb0ef41Sopenharmony_ci  // GC internal.
1621cb0ef41Sopenharmony_ci  V8_EXPORT_PRIVATE int SizeFromMap(Map map) const;
1631cb0ef41Sopenharmony_ci
1641cb0ef41Sopenharmony_ci  // Returns the field at offset in obj, as a read/write Object reference.
1651cb0ef41Sopenharmony_ci  // Does no checking, and is safe to use during GC, while maps are invalid.
1661cb0ef41Sopenharmony_ci  // Does not invoke write barrier, so should only be assigned to
1671cb0ef41Sopenharmony_ci  // during marking GC.
1681cb0ef41Sopenharmony_ci  inline ObjectSlot RawField(int byte_offset) const;
1691cb0ef41Sopenharmony_ci  inline MaybeObjectSlot RawMaybeWeakField(int byte_offset) const;
1701cb0ef41Sopenharmony_ci  inline CodeObjectSlot RawCodeField(int byte_offset) const;
1711cb0ef41Sopenharmony_ci  inline ExternalPointer_t RawExternalPointerField(int byte_offset) const;
1721cb0ef41Sopenharmony_ci
1731cb0ef41Sopenharmony_ci  DECL_CAST(HeapObject)
1741cb0ef41Sopenharmony_ci
1751cb0ef41Sopenharmony_ci  // Return the write barrier mode for this. Callers of this function
1761cb0ef41Sopenharmony_ci  // must be able to present a reference to an DisallowGarbageCollection
1771cb0ef41Sopenharmony_ci  // object as a sign that they are not going to use this function
1781cb0ef41Sopenharmony_ci  // from code that allocates and thus invalidates the returned write
1791cb0ef41Sopenharmony_ci  // barrier mode.
1801cb0ef41Sopenharmony_ci  inline WriteBarrierMode GetWriteBarrierMode(
1811cb0ef41Sopenharmony_ci      const DisallowGarbageCollection& promise);
1821cb0ef41Sopenharmony_ci
1831cb0ef41Sopenharmony_ci  // Dispatched behavior.
1841cb0ef41Sopenharmony_ci  void HeapObjectShortPrint(std::ostream& os);
1851cb0ef41Sopenharmony_ci#ifdef OBJECT_PRINT
1861cb0ef41Sopenharmony_ci  void PrintHeader(std::ostream& os, const char* id);
1871cb0ef41Sopenharmony_ci#endif
1881cb0ef41Sopenharmony_ci  DECL_PRINTER(HeapObject)
1891cb0ef41Sopenharmony_ci  EXPORT_DECL_VERIFIER(HeapObject)
1901cb0ef41Sopenharmony_ci#ifdef VERIFY_HEAP
1911cb0ef41Sopenharmony_ci  inline void VerifyObjectField(Isolate* isolate, int offset);
1921cb0ef41Sopenharmony_ci  inline void VerifySmiField(int offset);
1931cb0ef41Sopenharmony_ci  inline void VerifyMaybeObjectField(Isolate* isolate, int offset);
1941cb0ef41Sopenharmony_ci
1951cb0ef41Sopenharmony_ci  // Verify a pointer is a valid HeapObject pointer that points to object
1961cb0ef41Sopenharmony_ci  // areas in the heap.
1971cb0ef41Sopenharmony_ci  static void VerifyHeapPointer(Isolate* isolate, Object p);
1981cb0ef41Sopenharmony_ci  static void VerifyCodePointer(Isolate* isolate, Object p);
1991cb0ef41Sopenharmony_ci#endif
2001cb0ef41Sopenharmony_ci
2011cb0ef41Sopenharmony_ci  static inline AllocationAlignment RequiredAlignment(Map map);
2021cb0ef41Sopenharmony_ci
2031cb0ef41Sopenharmony_ci  // Whether the object needs rehashing. That is the case if the object's
2041cb0ef41Sopenharmony_ci  // content depends on FLAG_hash_seed. When the object is deserialized into
2051cb0ef41Sopenharmony_ci  // a heap with a different hash seed, these objects need to adapt.
2061cb0ef41Sopenharmony_ci  bool NeedsRehashing(InstanceType instance_type) const;
2071cb0ef41Sopenharmony_ci  bool NeedsRehashing(PtrComprCageBase cage_base) const;
2081cb0ef41Sopenharmony_ci
2091cb0ef41Sopenharmony_ci  // Rehashing support is not implemented for all objects that need rehashing.
2101cb0ef41Sopenharmony_ci  // With objects that need rehashing but cannot be rehashed, rehashing has to
2111cb0ef41Sopenharmony_ci  // be disabled.
2121cb0ef41Sopenharmony_ci  bool CanBeRehashed(PtrComprCageBase cage_base) const;
2131cb0ef41Sopenharmony_ci
2141cb0ef41Sopenharmony_ci  // Rehash the object based on the layout inferred from its map.
2151cb0ef41Sopenharmony_ci  template <typename IsolateT>
2161cb0ef41Sopenharmony_ci  void RehashBasedOnMap(IsolateT* isolate);
2171cb0ef41Sopenharmony_ci
2181cb0ef41Sopenharmony_ci  // Layout description.
2191cb0ef41Sopenharmony_ci#define HEAP_OBJECT_FIELDS(V) \
2201cb0ef41Sopenharmony_ci  V(kMapOffset, kTaggedSize)  \
2211cb0ef41Sopenharmony_ci  /* Header size. */          \
2221cb0ef41Sopenharmony_ci  V(kHeaderSize, 0)
2231cb0ef41Sopenharmony_ci
2241cb0ef41Sopenharmony_ci  DEFINE_FIELD_OFFSET_CONSTANTS(Object::kHeaderSize, HEAP_OBJECT_FIELDS)
2251cb0ef41Sopenharmony_ci#undef HEAP_OBJECT_FIELDS
2261cb0ef41Sopenharmony_ci
2271cb0ef41Sopenharmony_ci  STATIC_ASSERT(kMapOffset == Internals::kHeapObjectMapOffset);
2281cb0ef41Sopenharmony_ci
2291cb0ef41Sopenharmony_ci  using MapField = TaggedField<MapWord, HeapObject::kMapOffset>;
2301cb0ef41Sopenharmony_ci
2311cb0ef41Sopenharmony_ci  inline Address GetFieldAddress(int field_offset) const;
2321cb0ef41Sopenharmony_ci
2331cb0ef41Sopenharmony_ci protected:
2341cb0ef41Sopenharmony_ci  // Special-purpose constructor for subclasses that have fast paths where
2351cb0ef41Sopenharmony_ci  // their ptr() is a Smi.
2361cb0ef41Sopenharmony_ci  enum class AllowInlineSmiStorage { kRequireHeapObjectTag, kAllowBeingASmi };
2371cb0ef41Sopenharmony_ci  inline HeapObject(Address ptr, AllowInlineSmiStorage allow_smi);
2381cb0ef41Sopenharmony_ci
2391cb0ef41Sopenharmony_ci  OBJECT_CONSTRUCTORS(HeapObject, Object);
2401cb0ef41Sopenharmony_ci
2411cb0ef41Sopenharmony_ci private:
2421cb0ef41Sopenharmony_ci  enum class VerificationMode {
2431cb0ef41Sopenharmony_ci    kSafeMapTransition,
2441cb0ef41Sopenharmony_ci    kPotentialLayoutChange,
2451cb0ef41Sopenharmony_ci  };
2461cb0ef41Sopenharmony_ci
2471cb0ef41Sopenharmony_ci  enum class EmitWriteBarrier {
2481cb0ef41Sopenharmony_ci    kYes,
2491cb0ef41Sopenharmony_ci    kNo,
2501cb0ef41Sopenharmony_ci  };
2511cb0ef41Sopenharmony_ci
2521cb0ef41Sopenharmony_ci  template <EmitWriteBarrier emit_write_barrier, typename MemoryOrder>
2531cb0ef41Sopenharmony_ci  V8_INLINE void set_map(Map value, MemoryOrder order, VerificationMode mode);
2541cb0ef41Sopenharmony_ci};
2551cb0ef41Sopenharmony_ci
2561cb0ef41Sopenharmony_ciOBJECT_CONSTRUCTORS_IMPL(HeapObject, Object)
2571cb0ef41Sopenharmony_ciCAST_ACCESSOR(HeapObject)
2581cb0ef41Sopenharmony_ci
2591cb0ef41Sopenharmony_ci}  // namespace internal
2601cb0ef41Sopenharmony_ci}  // namespace v8
2611cb0ef41Sopenharmony_ci
2621cb0ef41Sopenharmony_ci#include "src/objects/object-macros-undef.h"
2631cb0ef41Sopenharmony_ci
2641cb0ef41Sopenharmony_ci#endif  // V8_OBJECTS_HEAP_OBJECT_H_
265