visitors.h 5.91 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 21
#define ROOT_ID_LIST(V)                                \
  V(kStringTable, "(Internalized strings)")            \
  V(kExternalStringsTable, "(External strings)")       \
Dan Elphick's avatar
Dan Elphick committed
22
  V(kReadOnlyRootList, "(Read-only roots)")            \
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
  V(kStrongRootList, "(Strong roots)")                 \
  V(kSmiRootList, "(Smi roots)")                       \
  V(kBootstrapper, "(Bootstrapper)")                   \
  V(kTop, "(Isolate)")                                 \
  V(kRelocatable, "(Relocatable)")                     \
  V(kDebug, "(Debugger)")                              \
  V(kCompilationCache, "(Compilation cache)")          \
  V(kHandleScope, "(Handle scope)")                    \
  V(kDispatchTable, "(Dispatch table)")                \
  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(kPartialSnapshotCache, "(Partial snapshot cache)") \
40
  V(kReadOnlyObjectCache, "(Read-only object cache)")  \
41 42 43
  V(kWeakCollections, "(Weak collections)")            \
  V(kWrapperTracing, "(Wrapper tracing)")              \
  V(kUnknown, "(Unknown)")
44 45 46

class VisitorSynchronization : public AllStatic {
 public:
47
#define DECLARE_ENUM(enum_item, ignore) enum_item,
48 49 50 51 52
  enum SyncTag { ROOT_ID_LIST(DECLARE_ENUM) kNumberOfSyncTags };
#undef DECLARE_ENUM
};

enum class Root {
53
#define DECLARE_ENUM(enum_item, ignore) enum_item,
54 55
  ROOT_ID_LIST(DECLARE_ENUM)
#undef DECLARE_ENUM
56
      kNumberOfRoots
57 58 59 60
};

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

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

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

  // 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.
  // TODO(ulan): Remove this.
  virtual void Synchronize(VisitorSynchronization::SyncTag tag) {}
81 82

  static const char* RootName(Root root);
83 84
};

85 86
class RelocIterator;

87 88
// Abstract base class for visiting, and optionally modifying, the
// pointers contained in Objects. Used in GC and serialization/deserialization.
89
class ObjectVisitor {
90
 public:
91
  virtual ~ObjectVisitor() = default;
92 93 94

  // Visits a contiguous arrays of pointers in the half-open range
  // [start, end). Any or all of the values may be modified on return.
95
  virtual void VisitPointers(HeapObject host, ObjectSlot start,
96
                             ObjectSlot end) = 0;
97
  virtual void VisitPointers(HeapObject host, MaybeObjectSlot start,
98
                             MaybeObjectSlot end) = 0;
99

100 101 102 103
  // 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.
104
  virtual void VisitCustomWeakPointers(HeapObject host, ObjectSlot start,
105
                                       ObjectSlot end) {
106 107 108
    VisitPointers(host, start, end);
  }

109
  // Handy shorthand for visiting a single pointer.
110
  virtual void VisitPointer(HeapObject host, ObjectSlot p) {
111 112
    VisitPointers(host, p, p + 1);
  }
113
  virtual void VisitPointer(HeapObject host, MaybeObjectSlot p) {
114 115
    VisitPointers(host, p, p + 1);
  }
116
  virtual void VisitCustomWeakPointer(HeapObject host, ObjectSlot p) {
117 118
    VisitCustomWeakPointers(host, p, p + 1);
  }
119

120 121 122 123 124 125
  virtual void VisitEphemeron(HeapObject host, int index, ObjectSlot key,
                              ObjectSlot value) {
    VisitPointer(host, key);
    VisitPointer(host, value);
  }

126
  // To allow lazy clearing of inline caches the visitor has
127
  // a rich interface for iterating over Code objects ...
128 129

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

132 133 134
  // Visit pointer embedded into a code object.
  virtual void VisitEmbeddedPointer(Code host, RelocInfo* rinfo) = 0;

135
  // Visits a runtime entry in the instruction stream.
136
  virtual void VisitRuntimeEntry(Code host, RelocInfo* rinfo) {}
137 138

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

  // Visits an external reference.
142
  virtual void VisitExternalReference(Foreign host, Address* p) {}
143 144

  // Visits an (encoded) internal reference.
145
  virtual void VisitInternalReference(Code host, RelocInfo* rinfo) {}
146 147

  // Visits an off-heap target in the instruction stream.
148
  virtual void VisitOffHeapTarget(Code host, RelocInfo* rinfo) {}
149 150 151

  // Visits the relocation info using the given iterator.
  virtual void VisitRelocInfo(RelocIterator* it);
152 153
};

154 155 156
}  // namespace internal
}  // namespace v8

157
#endif  // V8_OBJECTS_VISITORS_H_