visitors.h 5.69 KB
Newer Older
1 2 3 4 5 6 7
// 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.

#ifndef V8_VISITORS_H_
#define V8_VISITORS_H_

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

namespace v8 {
namespace internal {

16
class CodeDataContainer;
17
class MaybeObject;
18 19
class Object;

20 21 22
#define ROOT_ID_LIST(V)                                \
  V(kStringTable, "(Internalized strings)")            \
  V(kExternalStringsTable, "(External strings)")       \
Dan Elphick's avatar
Dan Elphick committed
23
  V(kReadOnlyRootList, "(Read-only roots)")            \
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
  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)") \
41
  V(kReadOnlyObjectCache, "(Read-only object cache)")  \
42 43 44
  V(kWeakCollections, "(Weak collections)")            \
  V(kWrapperTracing, "(Wrapper tracing)")              \
  V(kUnknown, "(Unknown)")
45 46 47

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

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

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

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

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

  // 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) {}
82 83

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

86 87
class RelocIterator;

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

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

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

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

  // To allow lazy clearing of inline caches the visitor has
122
  // a rich interface for iterating over Code objects ...
123 124

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

127 128 129
  // Visit pointer embedded into a code object.
  virtual void VisitEmbeddedPointer(Code host, RelocInfo* rinfo) = 0;

130
  // Visits a runtime entry in the instruction stream.
131
  virtual void VisitRuntimeEntry(Code host, RelocInfo* rinfo) {}
132 133

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

  // Visits an external reference.
137
  virtual void VisitExternalReference(Foreign host, Address* p) {}
138 139

  // Visits an (encoded) internal reference.
140
  virtual void VisitInternalReference(Code host, RelocInfo* rinfo) {}
141 142

  // Visits an off-heap target in the instruction stream.
143
  virtual void VisitOffHeapTarget(Code host, RelocInfo* rinfo) {}
144 145 146

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

149 150 151 152
}  // namespace internal
}  // namespace v8

#endif  // V8_VISITORS_H_