visitors.h 8.83 KB
Newer Older
1 2 3 4
// Copyright 2017 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_OBJECTS_VISITORS_H_
#define V8_OBJECTS_VISITORS_H_
7

8
#include "src/common/globals.h"
9
#include "src/objects/code.h"
10
#include "src/objects/compressed-slots.h"
11
#include "src/objects/foreign.h"
12
#include "src/objects/slots.h"
13 14 15 16

namespace v8 {
namespace internal {

17
class CodeDataContainer;
18

19 20
#define ROOT_ID_LIST(V)                                 \
  V(kStringTable, "(Internalized strings)")             \
21
  V(kStringForwardingTable, "(Forwarded strings)")      \
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
  V(kExternalStringsTable, "(External strings)")        \
  V(kReadOnlyRootList, "(Read-only roots)")             \
  V(kStrongRootList, "(Strong roots)")                  \
  V(kSmiRootList, "(Smi roots)")                        \
  V(kBootstrapper, "(Bootstrapper)")                    \
  V(kStackRoots, "(Stack roots)")                       \
  V(kRelocatable, "(Relocatable)")                      \
  V(kDebug, "(Debugger)")                               \
  V(kCompilationCache, "(Compilation cache)")           \
  V(kHandleScope, "(Handle scope)")                     \
  V(kBuiltins, "(Builtins)")                            \
  V(kGlobalHandles, "(Global handles)")                 \
  V(kEternalHandles, "(Eternal handles)")               \
  V(kThreadManager, "(Thread manager)")                 \
  V(kStrongRoots, "(Strong roots)")                     \
  V(kExtensions, "(Extensions)")                        \
  V(kCodeFlusher, "(Code flusher)")                     \
  V(kStartupObjectCache, "(Startup object cache)")      \
  V(kReadOnlyObjectCache, "(Read-only object cache)")   \
  V(kSharedHeapObjectCache, "(Shareable object cache)") \
  V(kWeakCollections, "(Weak collections)")             \
  V(kWrapperTracing, "(Wrapper tracing)")               \
  V(kWriteBarrier, "(Write barrier)")                   \
  V(kRetainMaps, "(Retain maps)")                       \
46
  V(kClientHeap, "(Client heap)")                       \
47
  V(kUnknown, "(Unknown)")
48 49 50

class VisitorSynchronization : public AllStatic {
 public:
51
#define DECLARE_ENUM(enum_item, ignore) enum_item,
52 53 54 55 56
  enum SyncTag { ROOT_ID_LIST(DECLARE_ENUM) kNumberOfSyncTags };
#undef DECLARE_ENUM
};

enum class Root {
57
#define DECLARE_ENUM(enum_item, ignore) enum_item,
58 59
  ROOT_ID_LIST(DECLARE_ENUM)
#undef DECLARE_ENUM
60
      kNumberOfRoots
61 62 63 64
};

// Abstract base class for visiting, and optionally modifying, the
// pointers contained in roots. Used in GC and serialization/deserialization.
65
class RootVisitor {
66
 public:
67
  virtual ~RootVisitor() = default;
68 69 70

  // Visits a contiguous arrays of pointers in the half-open range
  // [start, end). Any or all of the values may be modified on return.
71
  virtual void VisitRootPointers(Root root, const char* description,
72
                                 FullObjectSlot start, FullObjectSlot end) = 0;
73 74

  // Handy shorthand for visiting a single pointer.
75
  virtual void VisitRootPointer(Root root, const char* description,
76
                                FullObjectSlot p) {
77
    VisitRootPointers(root, description, p, p + 1);
78 79
  }

80 81 82 83 84 85 86 87 88 89 90 91 92 93
  // Visits a contiguous arrays of off-heap pointers in the half-open range
  // [start, end). Any or all of the values may be modified on return.
  virtual void VisitRootPointers(Root root, const char* description,
                                 OffHeapObjectSlot start,
                                 OffHeapObjectSlot end) {
    // This should be implemented for any visitor that visits the string table.
    // If we ever add new off-heap data-structures that we want to walk as roots
    // using this function, we should make it generic, by
    //
    //   1) Making this function pure virtual, and
    //   2) Implementing it for all visitors.
    UNREACHABLE();
  }

94 95 96 97 98 99 100
  // Visits a single pointer which is Code from the execution stack.
  virtual void VisitRunningCode(FullObjectSlot p) {
    // For most visitors, currently running Code is no different than any other
    // on-stack pointer.
    VisitRootPointer(Root::kStackRoots, nullptr, p);
  }

101 102 103 104
  // Intended for serialization/deserialization checking: insert, or
  // check for the presence of, a tag at this position in the stream.
  // Also used for marking up GC roots in heap snapshots.
  virtual void Synchronize(VisitorSynchronization::SyncTag tag) {}
105 106

  static const char* RootName(Root root);
107 108
};

109 110
class RelocIterator;

111 112
// Abstract base class for visiting, and optionally modifying, the
// pointers contained in Objects. Used in GC and serialization/deserialization.
113
class ObjectVisitor {
114
 public:
115
  virtual ~ObjectVisitor() = default;
116 117 118

  // Visits a contiguous arrays of pointers in the half-open range
  // [start, end). Any or all of the values may be modified on return.
119
  virtual void VisitPointers(HeapObject host, ObjectSlot start,
120
                             ObjectSlot end) = 0;
121
  virtual void VisitPointers(HeapObject host, MaybeObjectSlot start,
122
                             MaybeObjectSlot end) = 0;
123 124 125 126 127 128
  // When V8_EXTERNAL_CODE_SPACE is enabled, visits a Code pointer slot.
  // The values may be modified on return.
  // Not used when V8_EXTERNAL_CODE_SPACE is not enabled (the Code pointer
  // slots are visited as a part of on-heap slot visitation - via
  // VisitPointers()).
  virtual void VisitCodePointer(HeapObject host, CodeObjectSlot slot) = 0;
129

130 131 132 133
  // Custom weak pointers must be ignored by the GC but not other
  // visitors. They're used for e.g., lists that are recreated after GC. The
  // default implementation treats them as strong pointers. Visitors who want to
  // ignore them must override this function with empty.
134
  virtual void VisitCustomWeakPointers(HeapObject host, ObjectSlot start,
135
                                       ObjectSlot end) {
136 137 138
    VisitPointers(host, start, end);
  }

139
  // Handy shorthand for visiting a single pointer.
140
  virtual void VisitPointer(HeapObject host, ObjectSlot p) {
141 142
    VisitPointers(host, p, p + 1);
  }
143
  virtual void VisitPointer(HeapObject host, MaybeObjectSlot p) {
144 145
    VisitPointers(host, p, p + 1);
  }
146
  virtual void VisitCustomWeakPointer(HeapObject host, ObjectSlot p) {
147 148
    VisitCustomWeakPointers(host, p, p + 1);
  }
149

150 151 152 153 154 155
  virtual void VisitEphemeron(HeapObject host, int index, ObjectSlot key,
                              ObjectSlot value) {
    VisitPointer(host, key);
    VisitPointer(host, value);
  }

156
  // To allow lazy clearing of inline caches the visitor has
157
  // a rich interface for iterating over Code objects ...
158 159

  // Visits a code target in the instruction stream.
160
  virtual void VisitCodeTarget(Code host, RelocInfo* rinfo) = 0;
161

162 163 164
  // Visit pointer embedded into a code object.
  virtual void VisitEmbeddedPointer(Code host, RelocInfo* rinfo) = 0;

165
  // Visits a runtime entry in the instruction stream.
166
  virtual void VisitRuntimeEntry(Code host, RelocInfo* rinfo) {}
167 168

  // Visits an external reference embedded into a code object.
169
  virtual void VisitExternalReference(Code host, RelocInfo* rinfo) {}
170

171 172 173
  // Visits an external pointer.
  virtual void VisitExternalPointer(HeapObject host, ExternalPointerSlot slot,
                                    ExternalPointerTag tag) {}
174 175

  // Visits an (encoded) internal reference.
176
  virtual void VisitInternalReference(Code host, RelocInfo* rinfo) {}
177 178

  // Visits an off-heap target in the instruction stream.
179
  virtual void VisitOffHeapTarget(Code host, RelocInfo* rinfo) {}
180 181

  // Visits the relocation info using the given iterator.
182
  void VisitRelocInfo(RelocIterator* it);
183 184 185

  // Visits the object's map pointer, decoding as necessary
  virtual void VisitMapPointer(HeapObject host) { UNREACHABLE(); }
186 187
};

188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
// Helper version of ObjectVisitor that also takes care of caching base values
// of the main pointer compression cage and for the code cage.
class ObjectVisitorWithCageBases : public ObjectVisitor {
 public:
  inline ObjectVisitorWithCageBases(PtrComprCageBase cage_base,
                                    PtrComprCageBase code_cage_base);
  inline explicit ObjectVisitorWithCageBases(Isolate* isolate);
  inline explicit ObjectVisitorWithCageBases(Heap* heap);

  // The pointer compression cage base value used for decompression of all
  // tagged values except references to Code objects.
  PtrComprCageBase cage_base() const {
#if V8_COMPRESS_POINTERS
    return cage_base_;
#else
    return PtrComprCageBase{};
#endif  // V8_COMPRESS_POINTERS
  }

  // The pointer compression cage base value used for decompression of
  // references to Code objects.
  PtrComprCageBase code_cage_base() const {
210
#ifdef V8_EXTERNAL_CODE_SPACE
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
    return code_cage_base_;
#else
    return cage_base();
#endif  // V8_EXTERNAL_CODE_SPACE
  }

 private:
#if V8_COMPRESS_POINTERS
  const PtrComprCageBase cage_base_;
#ifdef V8_EXTERNAL_CODE_SPACE
  const PtrComprCageBase code_cage_base_;
#endif  // V8_EXTERNAL_CODE_SPACE
#endif  // V8_COMPRESS_POINTERS
};

226 227 228
}  // namespace internal
}  // namespace v8

229
#endif  // V8_OBJECTS_VISITORS_H_