Make some foo.h headers usable without foo-inl.h header.
[platform/upstream/v8.git] / src / heap / objects-visiting.h
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.
4
5 #ifndef V8_OBJECTS_VISITING_H_
6 #define V8_OBJECTS_VISITING_H_
7
8 #include "src/allocation.h"
9 #include "src/layout-descriptor.h"
10
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.
19
20 namespace v8 {
21 namespace internal {
22
23
24 // Base class for all static visitors.
25 class StaticVisitorBase : public AllStatic {
26  public:
27 #define VISITOR_ID_LIST(V) \
28   V(SeqOneByteString)      \
29   V(SeqTwoByteString)      \
30   V(ShortcutCandidate)     \
31   V(ByteArray)             \
32   V(BytecodeArray)         \
33   V(FreeSpace)             \
34   V(FixedArray)            \
35   V(FixedDoubleArray)      \
36   V(FixedTypedArray)       \
37   V(FixedFloat64Array)     \
38   V(NativeContext)         \
39   V(AllocationSite)        \
40   V(DataObject2)           \
41   V(DataObject3)           \
42   V(DataObject4)           \
43   V(DataObject5)           \
44   V(DataObject6)           \
45   V(DataObject7)           \
46   V(DataObject8)           \
47   V(DataObject9)           \
48   V(DataObjectGeneric)     \
49   V(JSObject2)             \
50   V(JSObject3)             \
51   V(JSObject4)             \
52   V(JSObject5)             \
53   V(JSObject6)             \
54   V(JSObject7)             \
55   V(JSObject8)             \
56   V(JSObject9)             \
57   V(JSObjectGeneric)       \
58   V(Struct2)               \
59   V(Struct3)               \
60   V(Struct4)               \
61   V(Struct5)               \
62   V(Struct6)               \
63   V(Struct7)               \
64   V(Struct8)               \
65   V(Struct9)               \
66   V(StructGeneric)         \
67   V(ConsString)            \
68   V(SlicedString)          \
69   V(Symbol)                \
70   V(Oddball)               \
71   V(Code)                  \
72   V(Map)                   \
73   V(Cell)                  \
74   V(PropertyCell)          \
75   V(WeakCell)              \
76   V(SharedFunctionInfo)    \
77   V(JSFunction)            \
78   V(JSWeakCollection)      \
79   V(JSArrayBuffer)         \
80   V(JSTypedArray)          \
81   V(JSDataView)            \
82   V(JSRegExp)
83
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.
93   enum VisitorId {
94 #define VISITOR_ID_ENUM_DECL(id) kVisit##id,
95     VISITOR_ID_LIST(VISITOR_ID_ENUM_DECL)
96 #undef VISITOR_ID_ENUM_DECL
97     kVisitorIdCount,
98     kVisitDataObject = kVisitDataObject2,
99     kVisitJSObject = kVisitJSObject2,
100     kVisitStruct = kVisitStruct2,
101     kMinObjectSizeInWords = 2
102   };
103
104   // Visitor ID should fit in one byte.
105   STATIC_ASSERT(kVisitorIdCount <= 256);
106
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);
111
112   // Determine which specialized visitor should be used for given map.
113   static VisitorId GetVisitorId(Map* map);
114
115   // For visitors that allow specialization by size calculate VisitorId based
116   // on size, base visitor id and generic visitor id.
117   static VisitorId GetVisitorIdForSize(VisitorId base, VisitorId generic,
118                                        int object_size,
119                                        bool has_unboxed_fields) {
120     DCHECK((base == kVisitDataObject) || (base == kVisitStruct) ||
121            (base == kVisitJSObject));
122     DCHECK(IsAligned(object_size, kPointerSize));
123     DCHECK(kMinObjectSizeInWords * kPointerSize <= object_size);
124     DCHECK(object_size <= Page::kMaxRegularHeapObjectSize);
125     DCHECK(!has_unboxed_fields || (base == kVisitJSObject));
126
127     if (has_unboxed_fields) return generic;
128
129     int visitor_id =
130         Min(base + (object_size >> kPointerSizeLog2) - kMinObjectSizeInWords,
131             static_cast<int>(generic));
132
133     return static_cast<VisitorId>(visitor_id);
134   }
135 };
136
137
138 template <typename Callback>
139 class VisitorDispatchTable {
140  public:
141   void CopyFrom(VisitorDispatchTable* other) {
142     // We are not using memcpy to guarantee that during update
143     // every element of callbacks_ array will remain correct
144     // pointer (memcpy might be implemented as a byte copying loop).
145     for (int i = 0; i < StaticVisitorBase::kVisitorIdCount; i++) {
146       base::NoBarrier_Store(&callbacks_[i], other->callbacks_[i]);
147     }
148   }
149
150   inline Callback GetVisitorById(StaticVisitorBase::VisitorId id) {
151     return reinterpret_cast<Callback>(callbacks_[id]);
152   }
153
154   inline Callback GetVisitor(Map* map) {
155     return reinterpret_cast<Callback>(callbacks_[map->visitor_id()]);
156   }
157
158   void Register(StaticVisitorBase::VisitorId id, Callback callback) {
159     DCHECK(id < StaticVisitorBase::kVisitorIdCount);  // id is unsigned.
160     callbacks_[id] = reinterpret_cast<base::AtomicWord>(callback);
161   }
162
163   template <typename Visitor, StaticVisitorBase::VisitorId base,
164             StaticVisitorBase::VisitorId generic, int object_size_in_words>
165   void RegisterSpecialization() {
166     static const int size = object_size_in_words * kPointerSize;
167     Register(StaticVisitorBase::GetVisitorIdForSize(base, generic, size, false),
168              &Visitor::template VisitSpecialized<size>);
169   }
170
171
172   template <typename Visitor, StaticVisitorBase::VisitorId base,
173             StaticVisitorBase::VisitorId generic>
174   void RegisterSpecializations() {
175     STATIC_ASSERT((generic - base + StaticVisitorBase::kMinObjectSizeInWords) ==
176                   10);
177     RegisterSpecialization<Visitor, base, generic, 2>();
178     RegisterSpecialization<Visitor, base, generic, 3>();
179     RegisterSpecialization<Visitor, base, generic, 4>();
180     RegisterSpecialization<Visitor, base, generic, 5>();
181     RegisterSpecialization<Visitor, base, generic, 6>();
182     RegisterSpecialization<Visitor, base, generic, 7>();
183     RegisterSpecialization<Visitor, base, generic, 8>();
184     RegisterSpecialization<Visitor, base, generic, 9>();
185     Register(generic, &Visitor::Visit);
186   }
187
188  private:
189   base::AtomicWord callbacks_[StaticVisitorBase::kVisitorIdCount];
190 };
191
192
193 template <typename StaticVisitor>
194 class BodyVisitorBase : public AllStatic {
195  public:
196   INLINE(static void IteratePointers(Heap* heap, HeapObject* object,
197                                      int start_offset, int end_offset)) {
198     DCHECK(!FLAG_unbox_double_fields || object->map()->HasFastPointerLayout());
199     IterateRawPointers(heap, object, start_offset, end_offset);
200   }
201
202   INLINE(static void IterateBody(Heap* heap, HeapObject* object,
203                                  int start_offset, int end_offset)) {
204     if (!FLAG_unbox_double_fields || object->map()->HasFastPointerLayout()) {
205       IterateRawPointers(heap, object, start_offset, end_offset);
206     } else {
207       IterateBodyUsingLayoutDescriptor(heap, object, start_offset, end_offset);
208     }
209   }
210
211  private:
212   INLINE(static void IterateRawPointers(Heap* heap, HeapObject* object,
213                                         int start_offset, int end_offset)) {
214     StaticVisitor::VisitPointers(heap, object,
215                                  HeapObject::RawField(object, start_offset),
216                                  HeapObject::RawField(object, end_offset));
217   }
218
219   static void IterateBodyUsingLayoutDescriptor(Heap* heap, HeapObject* object,
220                                                int start_offset,
221                                                int end_offset) {
222     DCHECK(FLAG_unbox_double_fields);
223     DCHECK(IsAligned(start_offset, kPointerSize) &&
224            IsAligned(end_offset, kPointerSize));
225
226     LayoutDescriptorHelper helper(object->map());
227     DCHECK(!helper.all_fields_tagged());
228     for (int offset = start_offset; offset < end_offset;) {
229       int end_of_region_offset;
230       if (helper.IsTagged(offset, end_offset, &end_of_region_offset)) {
231         IterateRawPointers(heap, object, offset, end_of_region_offset);
232       }
233       offset = end_of_region_offset;
234     }
235   }
236 };
237
238
239 template <typename StaticVisitor, typename BodyDescriptor, typename ReturnType>
240 class FlexibleBodyVisitor : public BodyVisitorBase<StaticVisitor> {
241  public:
242   INLINE(static ReturnType Visit(Map* map, HeapObject* object)) {
243     int object_size = BodyDescriptor::SizeOf(map, object);
244     BodyVisitorBase<StaticVisitor>::IterateBody(
245         map->GetHeap(), object, BodyDescriptor::kStartOffset, object_size);
246     return static_cast<ReturnType>(object_size);
247   }
248
249   template <int object_size>
250   static inline ReturnType VisitSpecialized(Map* map, HeapObject* object) {
251     DCHECK(BodyDescriptor::SizeOf(map, object) == object_size);
252     BodyVisitorBase<StaticVisitor>::IteratePointers(
253         map->GetHeap(), object, BodyDescriptor::kStartOffset, object_size);
254     return static_cast<ReturnType>(object_size);
255   }
256 };
257
258
259 template <typename StaticVisitor, typename BodyDescriptor, typename ReturnType>
260 class FixedBodyVisitor : public BodyVisitorBase<StaticVisitor> {
261  public:
262   INLINE(static ReturnType Visit(Map* map, HeapObject* object)) {
263     BodyVisitorBase<StaticVisitor>::IterateBody(map->GetHeap(), object,
264                                                 BodyDescriptor::kStartOffset,
265                                                 BodyDescriptor::kEndOffset);
266     return static_cast<ReturnType>(BodyDescriptor::kSize);
267   }
268 };
269
270
271 // Base class for visitors used for a linear new space iteration.
272 // IterateBody returns size of visited object.
273 // Certain types of objects (i.e. Code objects) are not handled
274 // by dispatch table of this visitor because they cannot appear
275 // in the new space.
276 //
277 // This class is intended to be used in the following way:
278 //
279 //   class SomeVisitor : public StaticNewSpaceVisitor<SomeVisitor> {
280 //     ...
281 //   }
282 //
283 // This is an example of Curiously recurring template pattern
284 // (see http://en.wikipedia.org/wiki/Curiously_recurring_template_pattern).
285 // We use CRTP to guarantee aggressive compile time optimizations (i.e.
286 // inlining and specialization of StaticVisitor::VisitPointers methods).
287 template <typename StaticVisitor>
288 class StaticNewSpaceVisitor : public StaticVisitorBase {
289  public:
290   static void Initialize();
291
292   INLINE(static int IterateBody(Map* map, HeapObject* obj)) {
293     return table_.GetVisitor(map)(map, obj);
294   }
295
296   INLINE(static void VisitPointers(Heap* heap, HeapObject* object,
297                                    Object** start, Object** end)) {
298     for (Object** p = start; p < end; p++) StaticVisitor::VisitPointer(heap, p);
299   }
300
301  private:
302   INLINE(static int VisitJSFunction(Map* map, HeapObject* object)) {
303     Heap* heap = map->GetHeap();
304     VisitPointers(heap, object,
305                   HeapObject::RawField(object, JSFunction::kPropertiesOffset),
306                   HeapObject::RawField(object, JSFunction::kCodeEntryOffset));
307
308     // Don't visit code entry. We are using this visitor only during scavenges.
309
310     VisitPointers(
311         heap, object, HeapObject::RawField(
312                           object, JSFunction::kCodeEntryOffset + kPointerSize),
313         HeapObject::RawField(object, JSFunction::kNonWeakFieldsEndOffset));
314     return JSFunction::kSize;
315   }
316
317   INLINE(static int VisitByteArray(Map* map, HeapObject* object)) {
318     return reinterpret_cast<ByteArray*>(object)->ByteArraySize();
319   }
320
321   INLINE(static int VisitBytecodeArray(Map* map, HeapObject* object)) {
322     return reinterpret_cast<BytecodeArray*>(object)->BytecodeArraySize();
323   }
324
325   INLINE(static int VisitFixedDoubleArray(Map* map, HeapObject* object)) {
326     int length = reinterpret_cast<FixedDoubleArray*>(object)->length();
327     return FixedDoubleArray::SizeFor(length);
328   }
329
330   INLINE(static int VisitFixedTypedArray(Map* map, HeapObject* object)) {
331     return reinterpret_cast<FixedTypedArrayBase*>(object)->size();
332   }
333
334   INLINE(static int VisitJSObject(Map* map, HeapObject* object)) {
335     return JSObjectVisitor::Visit(map, object);
336   }
337
338   INLINE(static int VisitSeqOneByteString(Map* map, HeapObject* object)) {
339     return SeqOneByteString::cast(object)
340         ->SeqOneByteStringSize(map->instance_type());
341   }
342
343   INLINE(static int VisitSeqTwoByteString(Map* map, HeapObject* object)) {
344     return SeqTwoByteString::cast(object)
345         ->SeqTwoByteStringSize(map->instance_type());
346   }
347
348   INLINE(static int VisitFreeSpace(Map* map, HeapObject* object)) {
349     return FreeSpace::cast(object)->Size();
350   }
351
352   INLINE(static int VisitJSArrayBuffer(Map* map, HeapObject* object));
353   INLINE(static int VisitJSTypedArray(Map* map, HeapObject* object));
354   INLINE(static int VisitJSDataView(Map* map, HeapObject* object));
355
356   class DataObjectVisitor {
357    public:
358     template <int object_size>
359     static inline int VisitSpecialized(Map* map, HeapObject* object) {
360       return object_size;
361     }
362
363     INLINE(static int Visit(Map* map, HeapObject* object)) {
364       return map->instance_size();
365     }
366   };
367
368   typedef FlexibleBodyVisitor<StaticVisitor, StructBodyDescriptor, int>
369       StructVisitor;
370
371   typedef FlexibleBodyVisitor<StaticVisitor, JSObject::BodyDescriptor, int>
372       JSObjectVisitor;
373
374   typedef int (*Callback)(Map* map, HeapObject* object);
375
376   static VisitorDispatchTable<Callback> table_;
377 };
378
379
380 template <typename StaticVisitor>
381 VisitorDispatchTable<typename StaticNewSpaceVisitor<StaticVisitor>::Callback>
382     StaticNewSpaceVisitor<StaticVisitor>::table_;
383
384
385 // Base class for visitors used to transitively mark the entire heap.
386 // IterateBody returns nothing.
387 // Certain types of objects might not be handled by this base class and
388 // no visitor function is registered by the generic initialization. A
389 // specialized visitor function needs to be provided by the inheriting
390 // class itself for those cases.
391 //
392 // This class is intended to be used in the following way:
393 //
394 //   class SomeVisitor : public StaticMarkingVisitor<SomeVisitor> {
395 //     ...
396 //   }
397 //
398 // This is an example of Curiously recurring template pattern.
399 template <typename StaticVisitor>
400 class StaticMarkingVisitor : public StaticVisitorBase {
401  public:
402   static void Initialize();
403
404   INLINE(static void IterateBody(Map* map, HeapObject* obj)) {
405     table_.GetVisitor(map)(map, obj);
406   }
407
408   INLINE(static void VisitPropertyCell(Map* map, HeapObject* object));
409   INLINE(static void VisitWeakCell(Map* map, HeapObject* object));
410   INLINE(static void VisitCodeEntry(Heap* heap, HeapObject* object,
411                                     Address entry_address));
412   INLINE(static void VisitEmbeddedPointer(Heap* heap, RelocInfo* rinfo));
413   INLINE(static void VisitCell(Heap* heap, RelocInfo* rinfo));
414   INLINE(static void VisitDebugTarget(Heap* heap, RelocInfo* rinfo));
415   INLINE(static void VisitCodeTarget(Heap* heap, RelocInfo* rinfo));
416   INLINE(static void VisitCodeAgeSequence(Heap* heap, RelocInfo* rinfo));
417   INLINE(static void VisitExternalReference(RelocInfo* rinfo)) {}
418   INLINE(static void VisitInternalReference(RelocInfo* rinfo)) {}
419   INLINE(static void VisitRuntimeEntry(RelocInfo* rinfo)) {}
420   // Skip the weak next code link in a code object.
421   INLINE(static void VisitNextCodeLink(Heap* heap, Object** slot)) {}
422
423   // Mark non-optimize code for functions inlined into the given optimized
424   // code. This will prevent it from being flushed.
425   static void MarkInlinedFunctionsCode(Heap* heap, Code* code);
426
427  protected:
428   INLINE(static void VisitMap(Map* map, HeapObject* object));
429   INLINE(static void VisitCode(Map* map, HeapObject* object));
430   INLINE(static void VisitSharedFunctionInfo(Map* map, HeapObject* object));
431   INLINE(static void VisitAllocationSite(Map* map, HeapObject* object));
432   INLINE(static void VisitWeakCollection(Map* map, HeapObject* object));
433   INLINE(static void VisitJSFunction(Map* map, HeapObject* object));
434   INLINE(static void VisitJSRegExp(Map* map, HeapObject* object));
435   INLINE(static void VisitJSArrayBuffer(Map* map, HeapObject* object));
436   INLINE(static void VisitJSTypedArray(Map* map, HeapObject* object));
437   INLINE(static void VisitJSDataView(Map* map, HeapObject* object));
438   INLINE(static void VisitNativeContext(Map* map, HeapObject* object));
439
440   // Mark pointers in a Map and its TransitionArray together, possibly
441   // treating transitions or back pointers weak.
442   static void MarkMapContents(Heap* heap, Map* map);
443   static void MarkTransitionArray(Heap* heap, TransitionArray* transitions);
444
445   // Mark pointers in the optimized code map that should act as strong
446   // references, possibly treating some entries weak.
447   static void MarkOptimizedCodeMap(Heap* heap, FixedArray* code_map);
448
449   // Code flushing support.
450   INLINE(static bool IsFlushable(Heap* heap, JSFunction* function));
451   INLINE(static bool IsFlushable(Heap* heap, SharedFunctionInfo* shared_info));
452
453   // Helpers used by code flushing support that visit pointer fields and treat
454   // references to code objects either strongly or weakly.
455   static void VisitSharedFunctionInfoStrongCode(Heap* heap, HeapObject* object);
456   static void VisitSharedFunctionInfoWeakCode(Heap* heap, HeapObject* object);
457   static void VisitJSFunctionStrongCode(Heap* heap, HeapObject* object);
458   static void VisitJSFunctionWeakCode(Heap* heap, HeapObject* object);
459
460   class DataObjectVisitor {
461    public:
462     template <int size>
463     static inline void VisitSpecialized(Map* map, HeapObject* object) {}
464
465     INLINE(static void Visit(Map* map, HeapObject* object)) {}
466   };
467
468   typedef FlexibleBodyVisitor<StaticVisitor, FixedArray::BodyDescriptor, void>
469       FixedArrayVisitor;
470
471   typedef FlexibleBodyVisitor<StaticVisitor, JSObject::BodyDescriptor, void>
472       JSObjectVisitor;
473
474   typedef FlexibleBodyVisitor<StaticVisitor, StructBodyDescriptor, void>
475       StructObjectVisitor;
476
477   typedef void (*Callback)(Map* map, HeapObject* object);
478
479   static VisitorDispatchTable<Callback> table_;
480 };
481
482
483 template <typename StaticVisitor>
484 VisitorDispatchTable<typename StaticMarkingVisitor<StaticVisitor>::Callback>
485     StaticMarkingVisitor<StaticVisitor>::table_;
486
487
488 class WeakObjectRetainer;
489
490
491 // A weak list is single linked list where each element has a weak pointer to
492 // the next element. Given the head of the list, this function removes dead
493 // elements from the list and if requested records slots for next-element
494 // pointers. The template parameter T is a WeakListVisitor that defines how to
495 // access the next-element pointers.
496 template <class T>
497 Object* VisitWeakList(Heap* heap, Object* list, WeakObjectRetainer* retainer);
498 }
499 }  // namespace v8::internal
500
501 #endif  // V8_OBJECTS_VISITING_H_