1 // Copyright 2012 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.
5 #ifndef V8_OBJECTS_VISITING_H_
6 #define V8_OBJECTS_VISITING_H_
8 #include "src/allocation.h"
9 #include "src/layout-descriptor.h"
11 // This file provides base classes and auxiliary methods for defining
12 // static object visitors used during GC.
13 // Visiting HeapObject body with a normal ObjectVisitor requires performing
14 // two switches on object's instance type to determine object size and layout
15 // and one or more virtual method calls on visitor itself.
16 // Static visitor is different: it provides a dispatch table which contains
17 // pointers to specialized visit functions. Each map has the visitor_id
18 // field which contains an index of specialized visitor to use.
24 // Base class for all static visitors.
25 class StaticVisitorBase : public AllStatic {
27 #define VISITOR_ID_LIST(V) \
30 V(ShortcutCandidate) \
37 V(FixedFloat64Array) \
48 V(DataObjectGeneric) \
76 V(SharedFunctionInfo) \
84 // For data objects, JS objects and structs along with generic visitor which
85 // can visit object of any size we provide visitors specialized by
86 // object size in words.
87 // Ids of specialized visitors are declared in a linear order (without
88 // holes) starting from the id of visitor specialized for 2 words objects
89 // (base visitor id) and ending with the id of generic visitor.
90 // Method GetVisitorIdForSize depends on this ordering to calculate visitor
91 // id of specialized visitor from given instance size, base visitor id and
92 // generic visitor's id.
94 #define VISITOR_ID_ENUM_DECL(id) kVisit##id,
95 VISITOR_ID_LIST(VISITOR_ID_ENUM_DECL)
96 #undef VISITOR_ID_ENUM_DECL
98 kVisitDataObject = kVisitDataObject2,
99 kVisitJSObject = kVisitJSObject2,
100 kVisitStruct = kVisitStruct2,
101 kMinObjectSizeInWords = 2
104 // Visitor ID should fit in one byte.
105 STATIC_ASSERT(kVisitorIdCount <= 256);
107 // Determine which specialized visitor should be used for given instance type
108 // and instance type.
109 static VisitorId GetVisitorId(int instance_type, int instance_size,
110 bool has_unboxed_fields);
112 // Determine which specialized visitor should be used for given map.
113 static VisitorId GetVisitorId(Map* map) {
115 map->instance_type(), map->instance_size(),
116 FLAG_unbox_double_fields && !map->HasFastPointerLayout());
119 // For visitors that allow specialization by size calculate VisitorId based
120 // on size, base visitor id and generic visitor id.
121 static VisitorId GetVisitorIdForSize(VisitorId base, VisitorId generic,
123 bool has_unboxed_fields) {
124 DCHECK((base == kVisitDataObject) || (base == kVisitStruct) ||
125 (base == kVisitJSObject));
126 DCHECK(IsAligned(object_size, kPointerSize));
127 DCHECK(kMinObjectSizeInWords * kPointerSize <= object_size);
128 DCHECK(object_size <= Page::kMaxRegularHeapObjectSize);
129 DCHECK(!has_unboxed_fields || (base == kVisitJSObject));
131 if (has_unboxed_fields) return generic;
134 Min(base + (object_size >> kPointerSizeLog2) - kMinObjectSizeInWords,
135 static_cast<int>(generic));
137 return static_cast<VisitorId>(visitor_id);
142 template <typename Callback>
143 class VisitorDispatchTable {
145 void CopyFrom(VisitorDispatchTable* other) {
146 // We are not using memcpy to guarantee that during update
147 // every element of callbacks_ array will remain correct
148 // pointer (memcpy might be implemented as a byte copying loop).
149 for (int i = 0; i < StaticVisitorBase::kVisitorIdCount; i++) {
150 base::NoBarrier_Store(&callbacks_[i], other->callbacks_[i]);
154 inline Callback GetVisitorById(StaticVisitorBase::VisitorId id) {
155 return reinterpret_cast<Callback>(callbacks_[id]);
158 inline Callback GetVisitor(Map* map) {
159 return reinterpret_cast<Callback>(callbacks_[map->visitor_id()]);
162 void Register(StaticVisitorBase::VisitorId id, Callback callback) {
163 DCHECK(id < StaticVisitorBase::kVisitorIdCount); // id is unsigned.
164 callbacks_[id] = reinterpret_cast<base::AtomicWord>(callback);
167 template <typename Visitor, StaticVisitorBase::VisitorId base,
168 StaticVisitorBase::VisitorId generic, int object_size_in_words>
169 void RegisterSpecialization() {
170 static const int size = object_size_in_words * kPointerSize;
171 Register(StaticVisitorBase::GetVisitorIdForSize(base, generic, size, false),
172 &Visitor::template VisitSpecialized<size>);
176 template <typename Visitor, StaticVisitorBase::VisitorId base,
177 StaticVisitorBase::VisitorId generic>
178 void RegisterSpecializations() {
179 STATIC_ASSERT((generic - base + StaticVisitorBase::kMinObjectSizeInWords) ==
181 RegisterSpecialization<Visitor, base, generic, 2>();
182 RegisterSpecialization<Visitor, base, generic, 3>();
183 RegisterSpecialization<Visitor, base, generic, 4>();
184 RegisterSpecialization<Visitor, base, generic, 5>();
185 RegisterSpecialization<Visitor, base, generic, 6>();
186 RegisterSpecialization<Visitor, base, generic, 7>();
187 RegisterSpecialization<Visitor, base, generic, 8>();
188 RegisterSpecialization<Visitor, base, generic, 9>();
189 Register(generic, &Visitor::Visit);
193 base::AtomicWord callbacks_[StaticVisitorBase::kVisitorIdCount];
197 template <typename StaticVisitor>
198 class BodyVisitorBase : public AllStatic {
200 INLINE(static void IteratePointers(Heap* heap, HeapObject* object,
201 int start_offset, int end_offset)) {
202 DCHECK(!FLAG_unbox_double_fields || object->map()->HasFastPointerLayout());
203 IterateRawPointers(heap, object, start_offset, end_offset);
206 INLINE(static void IterateBody(Heap* heap, HeapObject* object,
207 int start_offset, int end_offset)) {
208 if (!FLAG_unbox_double_fields || object->map()->HasFastPointerLayout()) {
209 IterateRawPointers(heap, object, start_offset, end_offset);
211 IterateBodyUsingLayoutDescriptor(heap, object, start_offset, end_offset);
216 INLINE(static void IterateRawPointers(Heap* heap, HeapObject* object,
217 int start_offset, int end_offset)) {
218 StaticVisitor::VisitPointers(heap, object,
219 HeapObject::RawField(object, start_offset),
220 HeapObject::RawField(object, end_offset));
223 static void IterateBodyUsingLayoutDescriptor(Heap* heap, HeapObject* object,
226 DCHECK(FLAG_unbox_double_fields);
227 DCHECK(IsAligned(start_offset, kPointerSize) &&
228 IsAligned(end_offset, kPointerSize));
230 LayoutDescriptorHelper helper(object->map());
231 DCHECK(!helper.all_fields_tagged());
232 for (int offset = start_offset; offset < end_offset;) {
233 int end_of_region_offset;
234 if (helper.IsTagged(offset, end_offset, &end_of_region_offset)) {
235 IterateRawPointers(heap, object, offset, end_of_region_offset);
237 offset = end_of_region_offset;
243 template <typename StaticVisitor, typename BodyDescriptor, typename ReturnType>
244 class FlexibleBodyVisitor : public BodyVisitorBase<StaticVisitor> {
246 INLINE(static ReturnType Visit(Map* map, HeapObject* object)) {
247 int object_size = BodyDescriptor::SizeOf(map, object);
248 BodyVisitorBase<StaticVisitor>::IterateBody(
249 map->GetHeap(), object, BodyDescriptor::kStartOffset, object_size);
250 return static_cast<ReturnType>(object_size);
253 template <int object_size>
254 static inline ReturnType VisitSpecialized(Map* map, HeapObject* object) {
255 DCHECK(BodyDescriptor::SizeOf(map, object) == object_size);
256 BodyVisitorBase<StaticVisitor>::IteratePointers(
257 map->GetHeap(), object, BodyDescriptor::kStartOffset, object_size);
258 return static_cast<ReturnType>(object_size);
263 template <typename StaticVisitor, typename BodyDescriptor, typename ReturnType>
264 class FixedBodyVisitor : public BodyVisitorBase<StaticVisitor> {
266 INLINE(static ReturnType Visit(Map* map, HeapObject* object)) {
267 BodyVisitorBase<StaticVisitor>::IterateBody(map->GetHeap(), object,
268 BodyDescriptor::kStartOffset,
269 BodyDescriptor::kEndOffset);
270 return static_cast<ReturnType>(BodyDescriptor::kSize);
275 // Base class for visitors used for a linear new space iteration.
276 // IterateBody returns size of visited object.
277 // Certain types of objects (i.e. Code objects) are not handled
278 // by dispatch table of this visitor because they cannot appear
281 // This class is intended to be used in the following way:
283 // class SomeVisitor : public StaticNewSpaceVisitor<SomeVisitor> {
287 // This is an example of Curiously recurring template pattern
288 // (see http://en.wikipedia.org/wiki/Curiously_recurring_template_pattern).
289 // We use CRTP to guarantee aggressive compile time optimizations (i.e.
290 // inlining and specialization of StaticVisitor::VisitPointers methods).
291 template <typename StaticVisitor>
292 class StaticNewSpaceVisitor : public StaticVisitorBase {
294 static void Initialize();
296 INLINE(static int IterateBody(Map* map, HeapObject* obj)) {
297 return table_.GetVisitor(map)(map, obj);
300 INLINE(static void VisitPointers(Heap* heap, HeapObject* object,
301 Object** start, Object** end)) {
302 for (Object** p = start; p < end; p++) StaticVisitor::VisitPointer(heap, p);
306 INLINE(static int VisitJSFunction(Map* map, HeapObject* object)) {
307 Heap* heap = map->GetHeap();
308 VisitPointers(heap, object,
309 HeapObject::RawField(object, JSFunction::kPropertiesOffset),
310 HeapObject::RawField(object, JSFunction::kCodeEntryOffset));
312 // Don't visit code entry. We are using this visitor only during scavenges.
315 heap, object, HeapObject::RawField(
316 object, JSFunction::kCodeEntryOffset + kPointerSize),
317 HeapObject::RawField(object, JSFunction::kNonWeakFieldsEndOffset));
318 return JSFunction::kSize;
321 INLINE(static int VisitByteArray(Map* map, HeapObject* object)) {
322 return reinterpret_cast<ByteArray*>(object)->ByteArraySize();
325 INLINE(static int VisitBytecodeArray(Map* map, HeapObject* object)) {
326 return reinterpret_cast<BytecodeArray*>(object)->BytecodeArraySize();
329 INLINE(static int VisitFixedDoubleArray(Map* map, HeapObject* object)) {
330 int length = reinterpret_cast<FixedDoubleArray*>(object)->length();
331 return FixedDoubleArray::SizeFor(length);
334 INLINE(static int VisitFixedTypedArray(Map* map, HeapObject* object)) {
335 return reinterpret_cast<FixedTypedArrayBase*>(object)->size();
338 INLINE(static int VisitJSObject(Map* map, HeapObject* object)) {
339 return JSObjectVisitor::Visit(map, object);
342 INLINE(static int VisitSeqOneByteString(Map* map, HeapObject* object)) {
343 return SeqOneByteString::cast(object)
344 ->SeqOneByteStringSize(map->instance_type());
347 INLINE(static int VisitSeqTwoByteString(Map* map, HeapObject* object)) {
348 return SeqTwoByteString::cast(object)
349 ->SeqTwoByteStringSize(map->instance_type());
352 INLINE(static int VisitFreeSpace(Map* map, HeapObject* object)) {
353 return FreeSpace::cast(object)->Size();
356 INLINE(static int VisitJSArrayBuffer(Map* map, HeapObject* object));
357 INLINE(static int VisitJSTypedArray(Map* map, HeapObject* object));
358 INLINE(static int VisitJSDataView(Map* map, HeapObject* object));
360 class DataObjectVisitor {
362 template <int object_size>
363 static inline int VisitSpecialized(Map* map, HeapObject* object) {
367 INLINE(static int Visit(Map* map, HeapObject* object)) {
368 return map->instance_size();
372 typedef FlexibleBodyVisitor<StaticVisitor, StructBodyDescriptor, int>
375 typedef FlexibleBodyVisitor<StaticVisitor, JSObject::BodyDescriptor, int>
378 typedef int (*Callback)(Map* map, HeapObject* object);
380 static VisitorDispatchTable<Callback> table_;
384 template <typename StaticVisitor>
385 VisitorDispatchTable<typename StaticNewSpaceVisitor<StaticVisitor>::Callback>
386 StaticNewSpaceVisitor<StaticVisitor>::table_;
389 // Base class for visitors used to transitively mark the entire heap.
390 // IterateBody returns nothing.
391 // Certain types of objects might not be handled by this base class and
392 // no visitor function is registered by the generic initialization. A
393 // specialized visitor function needs to be provided by the inheriting
394 // class itself for those cases.
396 // This class is intended to be used in the following way:
398 // class SomeVisitor : public StaticMarkingVisitor<SomeVisitor> {
402 // This is an example of Curiously recurring template pattern.
403 template <typename StaticVisitor>
404 class StaticMarkingVisitor : public StaticVisitorBase {
406 static void Initialize();
408 INLINE(static void IterateBody(Map* map, HeapObject* obj)) {
409 table_.GetVisitor(map)(map, obj);
412 INLINE(static void VisitPropertyCell(Map* map, HeapObject* object));
413 INLINE(static void VisitWeakCell(Map* map, HeapObject* object));
414 INLINE(static void VisitCodeEntry(Heap* heap, HeapObject* object,
415 Address entry_address));
416 INLINE(static void VisitEmbeddedPointer(Heap* heap, RelocInfo* rinfo));
417 INLINE(static void VisitCell(Heap* heap, RelocInfo* rinfo));
418 INLINE(static void VisitDebugTarget(Heap* heap, RelocInfo* rinfo));
419 INLINE(static void VisitCodeTarget(Heap* heap, RelocInfo* rinfo));
420 INLINE(static void VisitCodeAgeSequence(Heap* heap, RelocInfo* rinfo));
421 INLINE(static void VisitExternalReference(RelocInfo* rinfo)) {}
422 INLINE(static void VisitInternalReference(RelocInfo* rinfo)) {}
423 INLINE(static void VisitRuntimeEntry(RelocInfo* rinfo)) {}
424 // Skip the weak next code link in a code object.
425 INLINE(static void VisitNextCodeLink(Heap* heap, Object** slot)) {}
427 // Mark non-optimize code for functions inlined into the given optimized
428 // code. This will prevent it from being flushed.
429 static void MarkInlinedFunctionsCode(Heap* heap, Code* code);
432 INLINE(static void VisitMap(Map* map, HeapObject* object));
433 INLINE(static void VisitCode(Map* map, HeapObject* object));
434 INLINE(static void VisitSharedFunctionInfo(Map* map, HeapObject* object));
435 INLINE(static void VisitAllocationSite(Map* map, HeapObject* object));
436 INLINE(static void VisitWeakCollection(Map* map, HeapObject* object));
437 INLINE(static void VisitJSFunction(Map* map, HeapObject* object));
438 INLINE(static void VisitJSRegExp(Map* map, HeapObject* object));
439 INLINE(static void VisitJSArrayBuffer(Map* map, HeapObject* object));
440 INLINE(static void VisitJSTypedArray(Map* map, HeapObject* object));
441 INLINE(static void VisitJSDataView(Map* map, HeapObject* object));
442 INLINE(static void VisitNativeContext(Map* map, HeapObject* object));
444 // Mark pointers in a Map and its TransitionArray together, possibly
445 // treating transitions or back pointers weak.
446 static void MarkMapContents(Heap* heap, Map* map);
447 static void MarkTransitionArray(Heap* heap, TransitionArray* transitions);
449 // Mark pointers in the optimized code map that should act as strong
450 // references, possibly treating some entries weak.
451 static void MarkOptimizedCodeMap(Heap* heap, FixedArray* code_map);
453 // Code flushing support.
454 INLINE(static bool IsFlushable(Heap* heap, JSFunction* function));
455 INLINE(static bool IsFlushable(Heap* heap, SharedFunctionInfo* shared_info));
457 // Helpers used by code flushing support that visit pointer fields and treat
458 // references to code objects either strongly or weakly.
459 static void VisitSharedFunctionInfoStrongCode(Heap* heap, HeapObject* object);
460 static void VisitSharedFunctionInfoWeakCode(Heap* heap, HeapObject* object);
461 static void VisitJSFunctionStrongCode(Heap* heap, HeapObject* object);
462 static void VisitJSFunctionWeakCode(Heap* heap, HeapObject* object);
464 class DataObjectVisitor {
467 static inline void VisitSpecialized(Map* map, HeapObject* object) {}
469 INLINE(static void Visit(Map* map, HeapObject* object)) {}
472 typedef FlexibleBodyVisitor<StaticVisitor, FixedArray::BodyDescriptor, void>
475 typedef FlexibleBodyVisitor<StaticVisitor, JSObject::BodyDescriptor, void>
478 typedef FlexibleBodyVisitor<StaticVisitor, StructBodyDescriptor, void>
481 typedef void (*Callback)(Map* map, HeapObject* object);
483 static VisitorDispatchTable<Callback> table_;
487 template <typename StaticVisitor>
488 VisitorDispatchTable<typename StaticMarkingVisitor<StaticVisitor>::Callback>
489 StaticMarkingVisitor<StaticVisitor>::table_;
492 class WeakObjectRetainer;
495 // A weak list is single linked list where each element has a weak pointer to
496 // the next element. Given the head of the list, this function removes dead
497 // elements from the list and if requested records slots for next-element
498 // pointers. The template parameter T is a WeakListVisitor that defines how to
499 // access the next-element pointers.
501 Object* VisitWeakList(Heap* heap, Object* list, WeakObjectRetainer* retainer);
503 } // namespace v8::internal
505 #endif // V8_OBJECTS_VISITING_H_