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