visitors.h 5.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 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
#define ROOT_ID_LIST(V)                               \
  V(kStringTable, "(Internalized strings)")           \
  V(kExternalStringsTable, "(External strings)")      \
  V(kReadOnlyRootList, "(Read-only roots)")           \
  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(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(kWeakCollections, "(Weak collections)")           \
  V(kWrapperTracing, "(Wrapper tracing)")             \
42
  V(kUnknown, "(Unknown)")
43 44 45

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

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

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

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

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

  // 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) {}
80 81

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

84 85
class RelocIterator;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

153 154 155
}  // namespace internal
}  // namespace v8

156
#endif  // V8_OBJECTS_VISITORS_H_