1// Copyright 2017 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef V8_VISITORS_H_
6#define V8_VISITORS_H_
7
8#include "src/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
14namespace v8 {
15namespace internal {
16
17class CodeDataContainer;
18class MaybeObject;
19class Object;
20
21#define ROOT_ID_LIST(V) \
22 V(kStringTable, "(Internalized strings)") \
23 V(kExternalStringsTable, "(External strings)") \
24 V(kReadOnlyRootList, "(Read-only roots)") \
25 V(kStrongRootList, "(Strong roots)") \
26 V(kSmiRootList, "(Smi roots)") \
27 V(kBootstrapper, "(Bootstrapper)") \
28 V(kTop, "(Isolate)") \
29 V(kRelocatable, "(Relocatable)") \
30 V(kDebug, "(Debugger)") \
31 V(kCompilationCache, "(Compilation cache)") \
32 V(kHandleScope, "(Handle scope)") \
33 V(kDispatchTable, "(Dispatch table)") \
34 V(kBuiltins, "(Builtins)") \
35 V(kGlobalHandles, "(Global handles)") \
36 V(kEternalHandles, "(Eternal handles)") \
37 V(kThreadManager, "(Thread manager)") \
38 V(kStrongRoots, "(Strong roots)") \
39 V(kExtensions, "(Extensions)") \
40 V(kCodeFlusher, "(Code flusher)") \
41 V(kPartialSnapshotCache, "(Partial snapshot cache)") \
42 V(kReadOnlyObjectCache, "(Read-only object cache)") \
43 V(kWeakCollections, "(Weak collections)") \
44 V(kWrapperTracing, "(Wrapper tracing)") \
45 V(kUnknown, "(Unknown)")
46
47class VisitorSynchronization : public AllStatic {
48 public:
49#define DECLARE_ENUM(enum_item, ignore) enum_item,
50 enum SyncTag { ROOT_ID_LIST(DECLARE_ENUM) kNumberOfSyncTags };
51#undef DECLARE_ENUM
52};
53
54enum class Root {
55#define DECLARE_ENUM(enum_item, ignore) enum_item,
56 ROOT_ID_LIST(DECLARE_ENUM)
57#undef DECLARE_ENUM
58 kNumberOfRoots
59};
60
61// Abstract base class for visiting, and optionally modifying, the
62// pointers contained in roots. Used in GC and serialization/deserialization.
63class RootVisitor {
64 public:
65 virtual ~RootVisitor() = default;
66
67 // Visits a contiguous arrays of pointers in the half-open range
68 // [start, end). Any or all of the values may be modified on return.
69 virtual void VisitRootPointers(Root root, const char* description,
70 FullObjectSlot start, FullObjectSlot end) = 0;
71
72 // Handy shorthand for visiting a single pointer.
73 virtual void VisitRootPointer(Root root, const char* description,
74 FullObjectSlot p) {
75 VisitRootPointers(root, description, p, p + 1);
76 }
77
78 // Intended for serialization/deserialization checking: insert, or
79 // check for the presence of, a tag at this position in the stream.
80 // Also used for marking up GC roots in heap snapshots.
81 // TODO(ulan): Remove this.
82 virtual void Synchronize(VisitorSynchronization::SyncTag tag) {}
83
84 static const char* RootName(Root root);
85};
86
87class RelocIterator;
88
89// Abstract base class for visiting, and optionally modifying, the
90// pointers contained in Objects. Used in GC and serialization/deserialization.
91class ObjectVisitor {
92 public:
93 virtual ~ObjectVisitor() = default;
94
95 // Visits a contiguous arrays of pointers in the half-open range
96 // [start, end). Any or all of the values may be modified on return.
97 virtual void VisitPointers(HeapObject host, ObjectSlot start,
98 ObjectSlot end) = 0;
99 virtual void VisitPointers(HeapObject host, MaybeObjectSlot start,
100 MaybeObjectSlot end) = 0;
101
102 // Custom weak pointers must be ignored by the GC but not other
103 // visitors. They're used for e.g., lists that are recreated after GC. The
104 // default implementation treats them as strong pointers. Visitors who want to
105 // ignore them must override this function with empty.
106 virtual void VisitCustomWeakPointers(HeapObject host, ObjectSlot start,
107 ObjectSlot end) {
108 VisitPointers(host, start, end);
109 }
110
111 // Handy shorthand for visiting a single pointer.
112 virtual void VisitPointer(HeapObject host, ObjectSlot p) {
113 VisitPointers(host, p, p + 1);
114 }
115 virtual void VisitPointer(HeapObject host, MaybeObjectSlot p) {
116 VisitPointers(host, p, p + 1);
117 }
118 virtual void VisitCustomWeakPointer(HeapObject host, ObjectSlot p) {
119 VisitCustomWeakPointers(host, p, p + 1);
120 }
121
122 virtual void VisitEphemeron(HeapObject host, int index, ObjectSlot key,
123 ObjectSlot value) {
124 VisitPointer(host, key);
125 VisitPointer(host, value);
126 }
127
128 // To allow lazy clearing of inline caches the visitor has
129 // a rich interface for iterating over Code objects ...
130
131 // Visits a code target in the instruction stream.
132 virtual void VisitCodeTarget(Code host, RelocInfo* rinfo) = 0;
133
134 // Visit pointer embedded into a code object.
135 virtual void VisitEmbeddedPointer(Code host, RelocInfo* rinfo) = 0;
136
137 // Visits a runtime entry in the instruction stream.
138 virtual void VisitRuntimeEntry(Code host, RelocInfo* rinfo) {}
139
140 // Visits an external reference embedded into a code object.
141 virtual void VisitExternalReference(Code host, RelocInfo* rinfo) {}
142
143 // Visits an external reference.
144 virtual void VisitExternalReference(Foreign host, Address* p) {}
145
146 // Visits an (encoded) internal reference.
147 virtual void VisitInternalReference(Code host, RelocInfo* rinfo) {}
148
149 // Visits an off-heap target in the instruction stream.
150 virtual void VisitOffHeapTarget(Code host, RelocInfo* rinfo) {}
151
152 // Visits the relocation info using the given iterator.
153 virtual void VisitRelocInfo(RelocIterator* it);
154};
155
156} // namespace internal
157} // namespace v8
158
159#endif // V8_VISITORS_H_
160