blob: 315c897bec17e4706305fa99300ff601a0f3a3d7 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// Copyright 2011 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
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005#include "src/heap/objects-visiting.h"
6
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007#include "src/heap/mark-compact-inl.h"
8#include "src/heap/objects-visiting-inl.h"
9
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010namespace v8 {
11namespace internal {
12
13
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000014StaticVisitorBase::VisitorId StaticVisitorBase::GetVisitorId(Map* map) {
15 return GetVisitorId(map->instance_type(), map->instance_size(),
16 FLAG_unbox_double_fields && !map->HasFastPointerLayout());
17}
18
19
Ben Murdochb8a8cc12014-11-26 15:28:44 +000020StaticVisitorBase::VisitorId StaticVisitorBase::GetVisitorId(
Emily Bernierd0a1eb72015-03-24 16:35:39 -040021 int instance_type, int instance_size, bool has_unboxed_fields) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000022 if (instance_type < FIRST_NONSTRING_TYPE) {
23 switch (instance_type & kStringRepresentationMask) {
24 case kSeqStringTag:
25 if ((instance_type & kStringEncodingMask) == kOneByteStringTag) {
26 return kVisitSeqOneByteString;
27 } else {
28 return kVisitSeqTwoByteString;
29 }
30
31 case kConsStringTag:
32 if (IsShortcutCandidate(instance_type)) {
33 return kVisitShortcutCandidate;
34 } else {
35 return kVisitConsString;
36 }
37
38 case kSlicedStringTag:
39 return kVisitSlicedString;
40
41 case kExternalStringTag:
42 return GetVisitorIdForSize(kVisitDataObject, kVisitDataObjectGeneric,
Emily Bernierd0a1eb72015-03-24 16:35:39 -040043 instance_size, has_unboxed_fields);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000044 }
45 UNREACHABLE();
46 }
47
48 switch (instance_type) {
49 case BYTE_ARRAY_TYPE:
50 return kVisitByteArray;
51
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000052 case BYTECODE_ARRAY_TYPE:
53 return kVisitBytecodeArray;
54
Ben Murdochb8a8cc12014-11-26 15:28:44 +000055 case FREE_SPACE_TYPE:
56 return kVisitFreeSpace;
57
58 case FIXED_ARRAY_TYPE:
59 return kVisitFixedArray;
60
61 case FIXED_DOUBLE_ARRAY_TYPE:
62 return kVisitFixedDoubleArray;
63
Ben Murdochb8a8cc12014-11-26 15:28:44 +000064 case ODDBALL_TYPE:
65 return kVisitOddball;
66
67 case MAP_TYPE:
68 return kVisitMap;
69
70 case CODE_TYPE:
71 return kVisitCode;
72
73 case CELL_TYPE:
74 return kVisitCell;
75
76 case PROPERTY_CELL_TYPE:
77 return kVisitPropertyCell;
78
Emily Bernierd0a1eb72015-03-24 16:35:39 -040079 case WEAK_CELL_TYPE:
80 return kVisitWeakCell;
81
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000082 case TRANSITION_ARRAY_TYPE:
83 return kVisitTransitionArray;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000084
85 case JS_WEAK_MAP_TYPE:
86 case JS_WEAK_SET_TYPE:
87 return kVisitJSWeakCollection;
88
89 case JS_REGEXP_TYPE:
90 return kVisitJSRegExp;
91
92 case SHARED_FUNCTION_INFO_TYPE:
93 return kVisitSharedFunctionInfo;
94
95 case JS_PROXY_TYPE:
96 return GetVisitorIdForSize(kVisitStruct, kVisitStructGeneric,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000097 instance_size, has_unboxed_fields);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000098
99 case SYMBOL_TYPE:
100 return kVisitSymbol;
101
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000102 case JS_ARRAY_BUFFER_TYPE:
103 return kVisitJSArrayBuffer;
104
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000105 case JS_OBJECT_TYPE:
106 case JS_CONTEXT_EXTENSION_OBJECT_TYPE:
107 case JS_GENERATOR_OBJECT_TYPE:
108 case JS_MODULE_TYPE:
109 case JS_VALUE_TYPE:
110 case JS_DATE_TYPE:
111 case JS_ARRAY_TYPE:
112 case JS_GLOBAL_PROXY_TYPE:
113 case JS_GLOBAL_OBJECT_TYPE:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000114 case JS_MESSAGE_OBJECT_TYPE:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000115 case JS_TYPED_ARRAY_TYPE:
116 case JS_DATA_VIEW_TYPE:
117 case JS_SET_TYPE:
118 case JS_MAP_TYPE:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000119 case JS_SET_ITERATOR_TYPE:
120 case JS_MAP_ITERATOR_TYPE:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000121 case JS_ITERATOR_RESULT_TYPE:
122 case JS_PROMISE_TYPE:
123 case JS_BOUND_FUNCTION_TYPE:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000124 return GetVisitorIdForSize(kVisitJSObject, kVisitJSObjectGeneric,
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400125 instance_size, has_unboxed_fields);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000126
127 case JS_FUNCTION_TYPE:
128 return kVisitJSFunction;
129
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000130 case FILLER_TYPE:
131 if (instance_size == kPointerSize) return kVisitDataObjectGeneric;
132 // Fall through.
133 case FOREIGN_TYPE:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000134 case HEAP_NUMBER_TYPE:
135 case MUTABLE_HEAP_NUMBER_TYPE:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000136 case SIMD128_VALUE_TYPE:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000137 return GetVisitorIdForSize(kVisitDataObject, kVisitDataObjectGeneric,
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400138 instance_size, has_unboxed_fields);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000139
140 case FIXED_UINT8_ARRAY_TYPE:
141 case FIXED_INT8_ARRAY_TYPE:
142 case FIXED_UINT16_ARRAY_TYPE:
143 case FIXED_INT16_ARRAY_TYPE:
144 case FIXED_UINT32_ARRAY_TYPE:
145 case FIXED_INT32_ARRAY_TYPE:
146 case FIXED_FLOAT32_ARRAY_TYPE:
147 case FIXED_UINT8_CLAMPED_ARRAY_TYPE:
148 return kVisitFixedTypedArray;
149
150 case FIXED_FLOAT64_ARRAY_TYPE:
151 return kVisitFixedFloat64Array;
152
153#define MAKE_STRUCT_CASE(NAME, Name, name) case NAME##_TYPE:
154 STRUCT_LIST(MAKE_STRUCT_CASE)
155#undef MAKE_STRUCT_CASE
156 if (instance_type == ALLOCATION_SITE_TYPE) {
157 return kVisitAllocationSite;
158 }
159
160 return GetVisitorIdForSize(kVisitStruct, kVisitStructGeneric,
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400161 instance_size, has_unboxed_fields);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000162
163 default:
164 UNREACHABLE();
165 return kVisitorIdCount;
166 }
167}
168
169
170// We don't record weak slots during marking or scavenges. Instead we do it
171// once when we complete mark-compact cycle. Note that write barrier has no
172// effect if we are already in the middle of compacting mark-sweep cycle and we
173// have to record slots manually.
174static bool MustRecordSlots(Heap* heap) {
175 return heap->gc_state() == Heap::MARK_COMPACT &&
176 heap->mark_compact_collector()->is_compacting();
177}
178
179
180template <class T>
181struct WeakListVisitor;
182
183
184template <class T>
185Object* VisitWeakList(Heap* heap, Object* list, WeakObjectRetainer* retainer) {
186 Object* undefined = heap->undefined_value();
187 Object* head = undefined;
188 T* tail = NULL;
189 MarkCompactCollector* collector = heap->mark_compact_collector();
190 bool record_slots = MustRecordSlots(heap);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000191
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000192 while (list != undefined) {
193 // Check whether to keep the candidate in the list.
194 T* candidate = reinterpret_cast<T*>(list);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000195
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000196 Object* retained = retainer->RetainAs(list);
197 if (retained != NULL) {
198 if (head == undefined) {
199 // First element in the list.
200 head = retained;
201 } else {
202 // Subsequent elements in the list.
203 DCHECK(tail != NULL);
204 WeakListVisitor<T>::SetWeakNext(tail, retained);
205 if (record_slots) {
206 Object** next_slot =
207 HeapObject::RawField(tail, WeakListVisitor<T>::WeakNextOffset());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000208 collector->RecordSlot(tail, next_slot, retained);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000209 }
210 }
211 // Retained object is new tail.
212 DCHECK(!retained->IsUndefined());
213 candidate = reinterpret_cast<T*>(retained);
214 tail = candidate;
215
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000216 // tail is a live object, visit it.
217 WeakListVisitor<T>::VisitLiveObject(heap, tail, retainer);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000218
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000219 } else {
220 WeakListVisitor<T>::VisitPhantomObject(heap, candidate);
221 }
222
223 // Move to next element in the list.
224 list = WeakListVisitor<T>::WeakNext(candidate);
225 }
226
227 // Terminate the list if there is one or more elements.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000228 if (tail != NULL) WeakListVisitor<T>::SetWeakNext(tail, undefined);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000229 return head;
230}
231
232
233template <class T>
234static void ClearWeakList(Heap* heap, Object* list) {
235 Object* undefined = heap->undefined_value();
236 while (list != undefined) {
237 T* candidate = reinterpret_cast<T*>(list);
238 list = WeakListVisitor<T>::WeakNext(candidate);
239 WeakListVisitor<T>::SetWeakNext(candidate, undefined);
240 }
241}
242
243
244template <>
245struct WeakListVisitor<JSFunction> {
246 static void SetWeakNext(JSFunction* function, Object* next) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000247 function->set_next_function_link(next, UPDATE_WEAK_WRITE_BARRIER);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000248 }
249
250 static Object* WeakNext(JSFunction* function) {
251 return function->next_function_link();
252 }
253
254 static int WeakNextOffset() { return JSFunction::kNextFunctionLinkOffset; }
255
256 static void VisitLiveObject(Heap*, JSFunction*, WeakObjectRetainer*) {}
257
258 static void VisitPhantomObject(Heap*, JSFunction*) {}
259};
260
261
262template <>
263struct WeakListVisitor<Code> {
264 static void SetWeakNext(Code* code, Object* next) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000265 code->set_next_code_link(next, UPDATE_WEAK_WRITE_BARRIER);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000266 }
267
268 static Object* WeakNext(Code* code) { return code->next_code_link(); }
269
270 static int WeakNextOffset() { return Code::kNextCodeLinkOffset; }
271
272 static void VisitLiveObject(Heap*, Code*, WeakObjectRetainer*) {}
273
274 static void VisitPhantomObject(Heap*, Code*) {}
275};
276
277
278template <>
279struct WeakListVisitor<Context> {
280 static void SetWeakNext(Context* context, Object* next) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000281 context->set(Context::NEXT_CONTEXT_LINK, next, UPDATE_WEAK_WRITE_BARRIER);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000282 }
283
284 static Object* WeakNext(Context* context) {
285 return context->get(Context::NEXT_CONTEXT_LINK);
286 }
287
288 static int WeakNextOffset() {
289 return FixedArray::SizeFor(Context::NEXT_CONTEXT_LINK);
290 }
291
292 static void VisitLiveObject(Heap* heap, Context* context,
293 WeakObjectRetainer* retainer) {
294 // Process the three weak lists linked off the context.
295 DoWeakList<JSFunction>(heap, context, retainer,
296 Context::OPTIMIZED_FUNCTIONS_LIST);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000297
298 if (heap->gc_state() == Heap::MARK_COMPACT) {
299 // Record the slots of the weak entries in the native context.
300 MarkCompactCollector* collector = heap->mark_compact_collector();
301 for (int idx = Context::FIRST_WEAK_SLOT;
302 idx < Context::NATIVE_CONTEXT_SLOTS; ++idx) {
303 Object** slot = Context::cast(context)->RawFieldOfElementAt(idx);
304 collector->RecordSlot(context, slot, *slot);
305 }
306 // Code objects are always allocated in Code space, we do not have to
307 // visit
308 // them during scavenges.
309 DoWeakList<Code>(heap, context, retainer, Context::OPTIMIZED_CODE_LIST);
310 DoWeakList<Code>(heap, context, retainer, Context::DEOPTIMIZED_CODE_LIST);
311 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000312 }
313
314 template <class T>
315 static void DoWeakList(Heap* heap, Context* context,
316 WeakObjectRetainer* retainer, int index) {
317 // Visit the weak list, removing dead intermediate elements.
318 Object* list_head = VisitWeakList<T>(heap, context->get(index), retainer);
319
320 // Update the list head.
321 context->set(index, list_head, UPDATE_WRITE_BARRIER);
322
323 if (MustRecordSlots(heap)) {
324 // Record the updated slot if necessary.
325 Object** head_slot =
326 HeapObject::RawField(context, FixedArray::SizeFor(index));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000327 heap->mark_compact_collector()->RecordSlot(context, head_slot, list_head);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000328 }
329 }
330
331 static void VisitPhantomObject(Heap* heap, Context* context) {
332 ClearWeakList<JSFunction>(heap,
333 context->get(Context::OPTIMIZED_FUNCTIONS_LIST));
334 ClearWeakList<Code>(heap, context->get(Context::OPTIMIZED_CODE_LIST));
335 ClearWeakList<Code>(heap, context->get(Context::DEOPTIMIZED_CODE_LIST));
336 }
337};
338
339
340template <>
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000341struct WeakListVisitor<AllocationSite> {
342 static void SetWeakNext(AllocationSite* obj, Object* next) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000343 obj->set_weak_next(next, UPDATE_WEAK_WRITE_BARRIER);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000344 }
345
346 static Object* WeakNext(AllocationSite* obj) { return obj->weak_next(); }
347
348 static int WeakNextOffset() { return AllocationSite::kWeakNextOffset; }
349
350 static void VisitLiveObject(Heap*, AllocationSite*, WeakObjectRetainer*) {}
351
352 static void VisitPhantomObject(Heap*, AllocationSite*) {}
353};
354
355
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000356template Object* VisitWeakList<Context>(Heap* heap, Object* list,
357 WeakObjectRetainer* retainer);
358
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000359template Object* VisitWeakList<AllocationSite>(Heap* heap, Object* list,
360 WeakObjectRetainer* retainer);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000361} // namespace internal
362} // namespace v8