Add a write barrier for TVAR closures
[ghc.git] / rts / sm / Evac.c
1 /* -----------------------------------------------------------------------------
2 *
3 * (c) The GHC Team 1998-2008
4 *
5 * Generational garbage collector: evacuation functions
6 *
7 * Documentation on the architecture of the Garbage Collector can be
8 * found in the online commentary:
9 *
10 * http://hackage.haskell.org/trac/ghc/wiki/Commentary/Rts/Storage/GC
11 *
12 * ---------------------------------------------------------------------------*/
13
14 #include "PosixSource.h"
15 #include "Rts.h"
16
17 #include "Evac.h"
18 #include "Storage.h"
19 #include "GC.h"
20 #include "GCThread.h"
21 #include "GCTDecl.h"
22 #include "GCUtils.h"
23 #include "Compact.h"
24 #include "MarkStack.h"
25 #include "Prelude.h"
26 #include "Trace.h"
27 #include "LdvProfile.h"
28
29 #if defined(PROF_SPIN) && defined(THREADED_RTS) && defined(PARALLEL_GC)
30 StgWord64 whitehole_spin = 0;
31 #endif
32
33 #if defined(THREADED_RTS) && !defined(PARALLEL_GC)
34 #define evacuate(p) evacuate1(p)
35 #define HEAP_ALLOCED_GC(p) HEAP_ALLOCED(p)
36 #endif
37
38 #if !defined(PARALLEL_GC)
39 #define copy_tag_nolock(p, info, src, size, stp, tag) \
40 copy_tag(p, info, src, size, stp, tag)
41 #endif
42
43 /* Used to avoid long recursion due to selector thunks
44 */
45 #define MAX_THUNK_SELECTOR_DEPTH 16
46
47 static void eval_thunk_selector (StgClosure **q, StgSelector * p, rtsBool);
48 STATIC_INLINE void evacuate_large(StgPtr p);
49
50 /* -----------------------------------------------------------------------------
51 Allocate some space in which to copy an object.
52 -------------------------------------------------------------------------- */
53
54 STATIC_INLINE StgPtr
55 alloc_for_copy (nat size, nat gen_no)
56 {
57 StgPtr to;
58 gen_workspace *ws;
59
60 /* Find out where we're going, using the handy "to" pointer in
61 * the gen of the source object. If it turns out we need to
62 * evacuate to an older generation, adjust it here (see comment
63 * by evacuate()).
64 */
65 if (gen_no < gct->evac_gen_no) {
66 if (gct->eager_promotion) {
67 gen_no = gct->evac_gen_no;
68 } else {
69 gct->failed_to_evac = rtsTrue;
70 }
71 }
72
73 ws = &gct->gens[gen_no]; // zero memory references here
74
75 /* chain a new block onto the to-space for the destination gen if
76 * necessary.
77 */
78 to = ws->todo_free;
79 ws->todo_free += size;
80 if (ws->todo_free > ws->todo_lim) {
81 to = todo_block_full(size, ws);
82 }
83 ASSERT(ws->todo_free >= ws->todo_bd->free && ws->todo_free <= ws->todo_lim);
84
85 return to;
86 }
87
88 /* -----------------------------------------------------------------------------
89 The evacuate() code
90 -------------------------------------------------------------------------- */
91
92 STATIC_INLINE GNUC_ATTR_HOT void
93 copy_tag(StgClosure **p, const StgInfoTable *info,
94 StgClosure *src, nat size, nat gen_no, StgWord tag)
95 {
96 StgPtr to, from;
97 nat i;
98
99 to = alloc_for_copy(size,gen_no);
100
101 from = (StgPtr)src;
102 to[0] = (W_)info;
103 for (i = 1; i < size; i++) { // unroll for small i
104 to[i] = from[i];
105 }
106
107 // if (to+size+2 < bd->start + BLOCK_SIZE_W) {
108 // __builtin_prefetch(to + size + 2, 1);
109 // }
110
111 #if defined(PARALLEL_GC)
112 {
113 const StgInfoTable *new_info;
114 new_info = (const StgInfoTable *)cas((StgPtr)&src->header.info, (W_)info, MK_FORWARDING_PTR(to));
115 if (new_info != info) {
116 return evacuate(p); // does the failed_to_evac stuff
117 } else {
118 *p = TAG_CLOSURE(tag,(StgClosure*)to);
119 }
120 }
121 #else
122 src->header.info = (const StgInfoTable *)MK_FORWARDING_PTR(to);
123 *p = TAG_CLOSURE(tag,(StgClosure*)to);
124 #endif
125
126 #ifdef PROFILING
127 // We store the size of the just evacuated object in the LDV word so that
128 // the profiler can guess the position of the next object later.
129 SET_EVACUAEE_FOR_LDV(from, size);
130 #endif
131 }
132
133 #if defined(PARALLEL_GC)
134 STATIC_INLINE void
135 copy_tag_nolock(StgClosure **p, const StgInfoTable *info,
136 StgClosure *src, nat size, nat gen_no, StgWord tag)
137 {
138 StgPtr to, from;
139 nat i;
140
141 to = alloc_for_copy(size,gen_no);
142
143 from = (StgPtr)src;
144 to[0] = (W_)info;
145 for (i = 1; i < size; i++) { // unroll for small i
146 to[i] = from[i];
147 }
148
149 // if somebody else reads the forwarding pointer, we better make
150 // sure there's a closure at the end of it.
151 write_barrier();
152 *p = TAG_CLOSURE(tag,(StgClosure*)to);
153 src->header.info = (const StgInfoTable *)MK_FORWARDING_PTR(to);
154
155 // if (to+size+2 < bd->start + BLOCK_SIZE_W) {
156 // __builtin_prefetch(to + size + 2, 1);
157 // }
158
159 #ifdef PROFILING
160 // We store the size of the just evacuated object in the LDV word so that
161 // the profiler can guess the position of the next object later.
162 SET_EVACUAEE_FOR_LDV(from, size);
163 #endif
164 }
165 #endif
166
167 /* Special version of copy() for when we only want to copy the info
168 * pointer of an object, but reserve some padding after it. This is
169 * used to optimise evacuation of TSOs.
170 */
171 static rtsBool
172 copyPart(StgClosure **p, StgClosure *src, nat size_to_reserve,
173 nat size_to_copy, nat gen_no)
174 {
175 StgPtr to, from;
176 nat i;
177 StgWord info;
178
179 #if defined(PARALLEL_GC)
180 spin:
181 info = xchg((StgPtr)&src->header.info, (W_)&stg_WHITEHOLE_info);
182 if (info == (W_)&stg_WHITEHOLE_info) {
183 #ifdef PROF_SPIN
184 whitehole_spin++;
185 #endif
186 goto spin;
187 }
188 if (IS_FORWARDING_PTR(info)) {
189 src->header.info = (const StgInfoTable *)info;
190 evacuate(p); // does the failed_to_evac stuff
191 return rtsFalse;
192 }
193 #else
194 info = (W_)src->header.info;
195 #endif
196
197 to = alloc_for_copy(size_to_reserve, gen_no);
198
199 from = (StgPtr)src;
200 to[0] = info;
201 for (i = 1; i < size_to_copy; i++) { // unroll for small i
202 to[i] = from[i];
203 }
204
205 write_barrier();
206 src->header.info = (const StgInfoTable*)MK_FORWARDING_PTR(to);
207 *p = (StgClosure *)to;
208
209 #ifdef PROFILING
210 // We store the size of the just evacuated object in the LDV word so that
211 // the profiler can guess the position of the next object later.
212 SET_EVACUAEE_FOR_LDV(from, size_to_reserve);
213 // fill the slop
214 if (size_to_reserve - size_to_copy > 0)
215 LDV_FILL_SLOP(to + size_to_copy, (int)(size_to_reserve - size_to_copy));
216 #endif
217
218 return rtsTrue;
219 }
220
221
222 /* Copy wrappers that don't tag the closure after copying */
223 STATIC_INLINE GNUC_ATTR_HOT void
224 copy(StgClosure **p, const StgInfoTable *info,
225 StgClosure *src, nat size, nat gen_no)
226 {
227 copy_tag(p,info,src,size,gen_no,0);
228 }
229
230 /* -----------------------------------------------------------------------------
231 Evacuate a large object
232
233 This just consists of removing the object from the (doubly-linked)
234 gen->large_objects list, and linking it on to the (singly-linked)
235 gen->new_large_objects list, from where it will be scavenged later.
236
237 Convention: bd->flags has BF_EVACUATED set for a large object
238 that has been evacuated, or unset otherwise.
239 -------------------------------------------------------------------------- */
240
241 STATIC_INLINE void
242 evacuate_large(StgPtr p)
243 {
244 bdescr *bd;
245 generation *gen, *new_gen;
246 nat gen_no, new_gen_no;
247 gen_workspace *ws;
248
249 bd = Bdescr(p);
250 gen = bd->gen;
251 gen_no = bd->gen_no;
252 ACQUIRE_SPIN_LOCK(&gen->sync);
253
254 // already evacuated?
255 if (bd->flags & BF_EVACUATED) {
256 /* Don't forget to set the gct->failed_to_evac flag if we didn't get
257 * the desired destination (see comments in evacuate()).
258 */
259 if (gen_no < gct->evac_gen_no) {
260 gct->failed_to_evac = rtsTrue;
261 TICK_GC_FAILED_PROMOTION();
262 }
263 RELEASE_SPIN_LOCK(&gen->sync);
264 return;
265 }
266
267 // remove from large_object list
268 if (bd->u.back) {
269 bd->u.back->link = bd->link;
270 } else { // first object in the list
271 gen->large_objects = bd->link;
272 }
273 if (bd->link) {
274 bd->link->u.back = bd->u.back;
275 }
276
277 /* link it on to the evacuated large object list of the destination gen
278 */
279 new_gen_no = bd->dest_no;
280
281 if (new_gen_no < gct->evac_gen_no) {
282 if (gct->eager_promotion) {
283 new_gen_no = gct->evac_gen_no;
284 } else {
285 gct->failed_to_evac = rtsTrue;
286 }
287 }
288
289 ws = &gct->gens[new_gen_no];
290 new_gen = &generations[new_gen_no];
291
292 bd->flags |= BF_EVACUATED;
293 initBdescr(bd, new_gen, new_gen->to);
294
295 // If this is a block of pinned objects, we don't have to scan
296 // these objects, because they aren't allowed to contain any
297 // pointers. For these blocks, we skip the scavenge stage and put
298 // them straight on the scavenged_large_objects list.
299 if (bd->flags & BF_PINNED) {
300 ASSERT(get_itbl((StgClosure *)p)->type == ARR_WORDS);
301 if (new_gen != gen) { ACQUIRE_SPIN_LOCK(&new_gen->sync); }
302 dbl_link_onto(bd, &new_gen->scavenged_large_objects);
303 new_gen->n_scavenged_large_blocks += bd->blocks;
304 if (new_gen != gen) { RELEASE_SPIN_LOCK(&new_gen->sync); }
305 } else {
306 bd->link = ws->todo_large_objects;
307 ws->todo_large_objects = bd;
308 }
309
310 RELEASE_SPIN_LOCK(&gen->sync);
311 }
312
313 /* ----------------------------------------------------------------------------
314 Evacuate
315
316 This is called (eventually) for every live object in the system.
317
318 The caller to evacuate specifies a desired generation in the
319 gct->evac_gen thread-local variable. The following conditions apply to
320 evacuating an object which resides in generation M when we're
321 collecting up to generation N
322
323 if M >= gct->evac_gen
324 if M > N do nothing
325 else evac to gen->to
326
327 if M < gct->evac_gen evac to gct->evac_gen, step 0
328
329 if the object is already evacuated, then we check which generation
330 it now resides in.
331
332 if M >= gct->evac_gen do nothing
333 if M < gct->evac_gen set gct->failed_to_evac flag to indicate that we
334 didn't manage to evacuate this object into gct->evac_gen.
335
336
337 OPTIMISATION NOTES:
338
339 evacuate() is the single most important function performance-wise
340 in the GC. Various things have been tried to speed it up, but as
341 far as I can tell the code generated by gcc 3.2 with -O2 is about
342 as good as it's going to get. We pass the argument to evacuate()
343 in a register using the 'regparm' attribute (see the prototype for
344 evacuate() near the top of this file).
345
346 Changing evacuate() to take an (StgClosure **) rather than
347 returning the new pointer seems attractive, because we can avoid
348 writing back the pointer when it hasn't changed (eg. for a static
349 object, or an object in a generation > N). However, I tried it and
350 it doesn't help. One reason is that the (StgClosure **) pointer
351 gets spilled to the stack inside evacuate(), resulting in far more
352 extra reads/writes than we save.
353 ------------------------------------------------------------------------- */
354
355 REGPARM1 GNUC_ATTR_HOT void
356 evacuate(StgClosure **p)
357 {
358 bdescr *bd = NULL;
359 nat gen_no;
360 StgClosure *q;
361 const StgInfoTable *info;
362 StgWord tag;
363
364 q = *p;
365
366 loop:
367 /* The tag and the pointer are split, to be merged after evacing */
368 tag = GET_CLOSURE_TAG(q);
369 q = UNTAG_CLOSURE(q);
370
371 ASSERTM(LOOKS_LIKE_CLOSURE_PTR(q), "invalid closure, info=%p", q->header.info);
372
373 if (!HEAP_ALLOCED_GC(q)) {
374
375 if (!major_gc) return;
376
377 info = get_itbl(q);
378 switch (info->type) {
379
380 case THUNK_STATIC:
381 if (info->srt_bitmap != 0) {
382 if (*THUNK_STATIC_LINK((StgClosure *)q) == NULL) {
383 #ifndef THREADED_RTS
384 *THUNK_STATIC_LINK((StgClosure *)q) = gct->static_objects;
385 gct->static_objects = (StgClosure *)q;
386 #else
387 StgPtr link;
388 link = (StgPtr)cas((StgPtr)THUNK_STATIC_LINK((StgClosure *)q),
389 (StgWord)NULL,
390 (StgWord)gct->static_objects);
391 if (link == NULL) {
392 gct->static_objects = (StgClosure *)q;
393 }
394 #endif
395 }
396 }
397 return;
398
399 case FUN_STATIC:
400 if (info->srt_bitmap != 0 &&
401 *FUN_STATIC_LINK((StgClosure *)q) == NULL) {
402 #ifndef THREADED_RTS
403 *FUN_STATIC_LINK((StgClosure *)q) = gct->static_objects;
404 gct->static_objects = (StgClosure *)q;
405 #else
406 StgPtr link;
407 link = (StgPtr)cas((StgPtr)FUN_STATIC_LINK((StgClosure *)q),
408 (StgWord)NULL,
409 (StgWord)gct->static_objects);
410 if (link == NULL) {
411 gct->static_objects = (StgClosure *)q;
412 }
413 #endif
414 }
415 return;
416
417 case IND_STATIC:
418 /* If q->saved_info != NULL, then it's a revertible CAF - it'll be
419 * on the CAF list, so don't do anything with it here (we'll
420 * scavenge it later).
421 */
422 if (*IND_STATIC_LINK((StgClosure *)q) == NULL) {
423 #ifndef THREADED_RTS
424 *IND_STATIC_LINK((StgClosure *)q) = gct->static_objects;
425 gct->static_objects = (StgClosure *)q;
426 #else
427 StgPtr link;
428 link = (StgPtr)cas((StgPtr)IND_STATIC_LINK((StgClosure *)q),
429 (StgWord)NULL,
430 (StgWord)gct->static_objects);
431 if (link == NULL) {
432 gct->static_objects = (StgClosure *)q;
433 }
434 #endif
435 }
436 return;
437
438 case CONSTR_STATIC:
439 if (*STATIC_LINK(info,(StgClosure *)q) == NULL) {
440 #ifndef THREADED_RTS
441 *STATIC_LINK(info,(StgClosure *)q) = gct->static_objects;
442 gct->static_objects = (StgClosure *)q;
443 #else
444 StgPtr link;
445 link = (StgPtr)cas((StgPtr)STATIC_LINK(info,(StgClosure *)q),
446 (StgWord)NULL,
447 (StgWord)gct->static_objects);
448 if (link == NULL) {
449 gct->static_objects = (StgClosure *)q;
450 }
451 #endif
452 }
453 /* I am assuming that static_objects pointers are not
454 * written to other objects, and thus, no need to retag. */
455 return;
456
457 case CONSTR_NOCAF_STATIC:
458 /* no need to put these on the static linked list, they don't need
459 * to be scavenged.
460 */
461 return;
462
463 default:
464 barf("evacuate(static): strange closure type %d", (int)(info->type));
465 }
466 }
467
468 bd = Bdescr((P_)q);
469
470 if ((bd->flags & (BF_LARGE | BF_MARKED | BF_EVACUATED)) != 0) {
471
472 // pointer into to-space: just return it. It might be a pointer
473 // into a generation that we aren't collecting (> N), or it
474 // might just be a pointer into to-space. The latter doesn't
475 // happen often, but allowing it makes certain things a bit
476 // easier; e.g. scavenging an object is idempotent, so it's OK to
477 // have an object on the mutable list multiple times.
478 if (bd->flags & BF_EVACUATED) {
479 // We aren't copying this object, so we have to check
480 // whether it is already in the target generation. (this is
481 // the write barrier).
482 if (bd->gen_no < gct->evac_gen_no) {
483 gct->failed_to_evac = rtsTrue;
484 TICK_GC_FAILED_PROMOTION();
485 }
486 return;
487 }
488
489 /* evacuate large objects by re-linking them onto a different list.
490 */
491 if (bd->flags & BF_LARGE) {
492 evacuate_large((P_)q);
493 return;
494 }
495
496 /* If the object is in a gen that we're compacting, then we
497 * need to use an alternative evacuate procedure.
498 */
499 if (!is_marked((P_)q,bd)) {
500 mark((P_)q,bd);
501 push_mark_stack((P_)q);
502 }
503 return;
504 }
505
506 gen_no = bd->dest_no;
507
508 info = q->header.info;
509 if (IS_FORWARDING_PTR(info))
510 {
511 /* Already evacuated, just return the forwarding address.
512 * HOWEVER: if the requested destination generation (gct->evac_gen) is
513 * older than the actual generation (because the object was
514 * already evacuated to a younger generation) then we have to
515 * set the gct->failed_to_evac flag to indicate that we couldn't
516 * manage to promote the object to the desired generation.
517 */
518 /*
519 * Optimisation: the check is fairly expensive, but we can often
520 * shortcut it if either the required generation is 0, or the
521 * current object (the EVACUATED) is in a high enough generation.
522 * We know that an EVACUATED always points to an object in the
523 * same or an older generation. gen is the lowest generation that the
524 * current object would be evacuated to, so we only do the full
525 * check if gen is too low.
526 */
527 StgClosure *e = (StgClosure*)UN_FORWARDING_PTR(info);
528 *p = TAG_CLOSURE(tag,e);
529 if (gen_no < gct->evac_gen_no) { // optimisation
530 if (Bdescr((P_)e)->gen_no < gct->evac_gen_no) {
531 gct->failed_to_evac = rtsTrue;
532 TICK_GC_FAILED_PROMOTION();
533 }
534 }
535 return;
536 }
537
538 switch (INFO_PTR_TO_STRUCT(info)->type) {
539
540 case WHITEHOLE:
541 goto loop;
542
543 // For ints and chars of low value, save space by replacing references to
544 // these with closures with references to common, shared ones in the RTS.
545 //
546 // * Except when compiling into Windows DLLs which don't support cross-package
547 // data references very well.
548 //
549 case CONSTR_0_1:
550 {
551 #if defined(COMPILING_WINDOWS_DLL)
552 copy_tag_nolock(p,info,q,sizeofW(StgHeader)+1,gen_no,tag);
553 #else
554 StgWord w = (StgWord)q->payload[0];
555 if (info == Czh_con_info &&
556 // unsigned, so always true: (StgChar)w >= MIN_CHARLIKE &&
557 (StgChar)w <= MAX_CHARLIKE) {
558 *p = TAG_CLOSURE(tag,
559 (StgClosure *)CHARLIKE_CLOSURE((StgChar)w)
560 );
561 }
562 else if (info == Izh_con_info &&
563 (StgInt)w >= MIN_INTLIKE && (StgInt)w <= MAX_INTLIKE) {
564 *p = TAG_CLOSURE(tag,
565 (StgClosure *)INTLIKE_CLOSURE((StgInt)w)
566 );
567 }
568 else {
569 copy_tag_nolock(p,info,q,sizeofW(StgHeader)+1,gen_no,tag);
570 }
571 #endif
572 return;
573 }
574
575 case FUN_0_1:
576 case FUN_1_0:
577 case CONSTR_1_0:
578 copy_tag_nolock(p,info,q,sizeofW(StgHeader)+1,gen_no,tag);
579 return;
580
581 case THUNK_1_0:
582 case THUNK_0_1:
583 copy(p,info,q,sizeofW(StgThunk)+1,gen_no);
584 return;
585
586 case THUNK_1_1:
587 case THUNK_2_0:
588 case THUNK_0_2:
589 #ifdef NO_PROMOTE_THUNKS
590 #error bitrotted
591 #endif
592 copy(p,info,q,sizeofW(StgThunk)+2,gen_no);
593 return;
594
595 case FUN_1_1:
596 case FUN_2_0:
597 case FUN_0_2:
598 case CONSTR_1_1:
599 case CONSTR_2_0:
600 copy_tag_nolock(p,info,q,sizeofW(StgHeader)+2,gen_no,tag);
601 return;
602
603 case CONSTR_0_2:
604 copy_tag_nolock(p,info,q,sizeofW(StgHeader)+2,gen_no,tag);
605 return;
606
607 case THUNK:
608 copy(p,info,q,thunk_sizeW_fromITBL(INFO_PTR_TO_STRUCT(info)),gen_no);
609 return;
610
611 case FUN:
612 case IND_PERM:
613 case CONSTR:
614 copy_tag_nolock(p,info,q,sizeW_fromITBL(INFO_PTR_TO_STRUCT(info)),gen_no,tag);
615 return;
616
617 case BLACKHOLE:
618 {
619 StgClosure *r;
620 const StgInfoTable *i;
621 r = ((StgInd*)q)->indirectee;
622 if (GET_CLOSURE_TAG(r) == 0) {
623 i = r->header.info;
624 if (IS_FORWARDING_PTR(i)) {
625 r = (StgClosure *)UN_FORWARDING_PTR(i);
626 i = r->header.info;
627 }
628 if (i == &stg_TSO_info
629 || i == &stg_WHITEHOLE_info
630 || i == &stg_BLOCKING_QUEUE_CLEAN_info
631 || i == &stg_BLOCKING_QUEUE_DIRTY_info) {
632 copy(p,info,q,sizeofW(StgInd),gen_no);
633 return;
634 }
635 ASSERT(i != &stg_IND_info);
636 }
637 q = r;
638 *p = r;
639 goto loop;
640 }
641
642 case MUT_VAR_CLEAN:
643 case MUT_VAR_DIRTY:
644 case MVAR_CLEAN:
645 case MVAR_DIRTY:
646 case TVAR:
647 case BLOCKING_QUEUE:
648 case WEAK:
649 case PRIM:
650 case MUT_PRIM:
651 copy(p,info,q,sizeW_fromITBL(INFO_PTR_TO_STRUCT(info)),gen_no);
652 return;
653
654 case BCO:
655 copy(p,info,q,bco_sizeW((StgBCO *)q),gen_no);
656 return;
657
658 case THUNK_SELECTOR:
659 eval_thunk_selector(p, (StgSelector *)q, rtsTrue);
660 return;
661
662 case IND:
663 // follow chains of indirections, don't evacuate them
664 q = ((StgInd*)q)->indirectee;
665 *p = q;
666 goto loop;
667
668 case RET_BCO:
669 case RET_SMALL:
670 case RET_BIG:
671 case UPDATE_FRAME:
672 case UNDERFLOW_FRAME:
673 case STOP_FRAME:
674 case CATCH_FRAME:
675 case CATCH_STM_FRAME:
676 case CATCH_RETRY_FRAME:
677 case ATOMICALLY_FRAME:
678 // shouldn't see these
679 barf("evacuate: stack frame at %p\n", q);
680
681 case PAP:
682 copy(p,info,q,pap_sizeW((StgPAP*)q),gen_no);
683 return;
684
685 case AP:
686 copy(p,info,q,ap_sizeW((StgAP*)q),gen_no);
687 return;
688
689 case AP_STACK:
690 copy(p,info,q,ap_stack_sizeW((StgAP_STACK*)q),gen_no);
691 return;
692
693 case ARR_WORDS:
694 // just copy the block
695 copy(p,info,q,arr_words_sizeW((StgArrWords *)q),gen_no);
696 return;
697
698 case MUT_ARR_PTRS_CLEAN:
699 case MUT_ARR_PTRS_DIRTY:
700 case MUT_ARR_PTRS_FROZEN:
701 case MUT_ARR_PTRS_FROZEN0:
702 // just copy the block
703 copy(p,info,q,mut_arr_ptrs_sizeW((StgMutArrPtrs *)q),gen_no);
704 return;
705
706 case TSO:
707 copy(p,info,q,sizeofW(StgTSO),gen_no);
708 return;
709
710 case STACK:
711 {
712 StgStack *stack = (StgStack *)q;
713
714 /* To evacuate a small STACK, we need to adjust the stack pointer
715 */
716 {
717 StgStack *new_stack;
718 StgPtr r, s;
719 rtsBool mine;
720
721 mine = copyPart(p,(StgClosure *)stack, stack_sizeW(stack),
722 sizeofW(StgStack), gen_no);
723 if (mine) {
724 new_stack = (StgStack *)*p;
725 move_STACK(stack, new_stack);
726 for (r = stack->sp, s = new_stack->sp;
727 r < stack->stack + stack->stack_size;) {
728 *s++ = *r++;
729 }
730 }
731 return;
732 }
733 }
734
735 case TREC_CHUNK:
736 copy(p,info,q,sizeofW(StgTRecChunk),gen_no);
737 return;
738
739 default:
740 barf("evacuate: strange closure type %d", (int)(INFO_PTR_TO_STRUCT(info)->type));
741 }
742
743 barf("evacuate");
744 }
745
746 /* -----------------------------------------------------------------------------
747 Evaluate a THUNK_SELECTOR if possible.
748
749 p points to a THUNK_SELECTOR that we want to evaluate. The
750 result of "evaluating" it will be evacuated and a pointer to the
751 to-space closure will be returned.
752
753 If the THUNK_SELECTOR could not be evaluated (its selectee is still
754 a THUNK, for example), then the THUNK_SELECTOR itself will be
755 evacuated.
756 -------------------------------------------------------------------------- */
757 static void
758 unchain_thunk_selectors(StgSelector *p, StgClosure *val)
759 {
760 StgSelector *prev;
761
762 prev = NULL;
763 while (p)
764 {
765 ASSERT(p->header.info == &stg_WHITEHOLE_info);
766 // val must be in to-space. Not always: when we recursively
767 // invoke eval_thunk_selector(), the recursive calls will not
768 // evacuate the value (because we want to select on the value,
769 // not evacuate it), so in this case val is in from-space.
770 // ASSERT(!HEAP_ALLOCED_GC(val) || Bdescr((P_)val)->gen_no > N || (Bdescr((P_)val)->flags & BF_EVACUATED));
771
772 prev = (StgSelector*)((StgClosure *)p)->payload[0];
773
774 // Update the THUNK_SELECTOR with an indirection to the
775 // value. The value is still in from-space at this stage.
776 //
777 // (old note: Why not do upd_evacuee(q,p)? Because we have an
778 // invariant that an EVACUATED closure always points to an
779 // object in the same or an older generation (required by
780 // the short-cut test in the EVACUATED case, below).
781 if ((StgClosure *)p == val) {
782 // must be a loop; just leave a BLACKHOLE in place. This
783 // can happen when we have a chain of selectors that
784 // eventually loops back on itself. We can't leave an
785 // indirection pointing to itself, and we want the program
786 // to deadlock if it ever enters this closure, so
787 // BLACKHOLE is correct.
788
789 // XXX we do not have BLACKHOLEs any more; replace with
790 // a THUNK_SELECTOR again. This will go into a loop if it is
791 // entered, and should result in a NonTermination exception.
792 ((StgThunk *)p)->payload[0] = val;
793 write_barrier();
794 SET_INFO((StgClosure *)p, &stg_sel_0_upd_info);
795 } else {
796 ((StgInd *)p)->indirectee = val;
797 write_barrier();
798 SET_INFO((StgClosure *)p, &stg_IND_info);
799 }
800
801 // For the purposes of LDV profiling, we have created an
802 // indirection.
803 LDV_RECORD_CREATE(p);
804
805 p = prev;
806 }
807 }
808
809 static void
810 eval_thunk_selector (StgClosure **q, StgSelector * p, rtsBool evac)
811 // NB. for legacy reasons, p & q are swapped around :(
812 {
813 nat field;
814 StgInfoTable *info;
815 StgWord info_ptr;
816 StgClosure *selectee;
817 StgSelector *prev_thunk_selector;
818 bdescr *bd;
819 StgClosure *val;
820
821 prev_thunk_selector = NULL;
822 // this is a chain of THUNK_SELECTORs that we are going to update
823 // to point to the value of the current THUNK_SELECTOR. Each
824 // closure on the chain is a WHITEHOLE, and points to the next in the
825 // chain with payload[0].
826
827 selector_chain:
828
829 bd = Bdescr((StgPtr)p);
830 if (HEAP_ALLOCED_GC(p)) {
831 // If the THUNK_SELECTOR is in to-space or in a generation that we
832 // are not collecting, then bale out early. We won't be able to
833 // save any space in any case, and updating with an indirection is
834 // trickier in a non-collected gen: we would have to update the
835 // mutable list.
836 if (bd->flags & BF_EVACUATED) {
837 unchain_thunk_selectors(prev_thunk_selector, (StgClosure *)p);
838 *q = (StgClosure *)p;
839 // shortcut, behave as for: if (evac) evacuate(q);
840 if (evac && bd->gen_no < gct->evac_gen_no) {
841 gct->failed_to_evac = rtsTrue;
842 TICK_GC_FAILED_PROMOTION();
843 }
844 return;
845 }
846 // we don't update THUNK_SELECTORS in the compacted
847 // generation, because compaction does not remove the INDs
848 // that result, this causes confusion later
849 // (scavenge_mark_stack doesn't deal with IND). BEWARE! This
850 // bit is very tricky to get right. If you make changes
851 // around here, test by compiling stage 3 with +RTS -c -RTS.
852 if (bd->flags & BF_MARKED) {
853 // must call evacuate() to mark this closure if evac==rtsTrue
854 *q = (StgClosure *)p;
855 if (evac) evacuate(q);
856 unchain_thunk_selectors(prev_thunk_selector, (StgClosure *)p);
857 return;
858 }
859 }
860
861
862 // WHITEHOLE the selector thunk, since it is now under evaluation.
863 // This is important to stop us going into an infinite loop if
864 // this selector thunk eventually refers to itself.
865 #if defined(THREADED_RTS)
866 // In threaded mode, we'll use WHITEHOLE to lock the selector
867 // thunk while we evaluate it.
868 {
869 do {
870 info_ptr = xchg((StgPtr)&p->header.info, (W_)&stg_WHITEHOLE_info);
871 } while (info_ptr == (W_)&stg_WHITEHOLE_info);
872
873 // make sure someone else didn't get here first...
874 if (IS_FORWARDING_PTR(info_ptr) ||
875 INFO_PTR_TO_STRUCT((StgInfoTable *)info_ptr)->type != THUNK_SELECTOR) {
876 // v. tricky now. The THUNK_SELECTOR has been evacuated
877 // by another thread, and is now either a forwarding ptr or IND.
878 // We need to extract ourselves from the current situation
879 // as cleanly as possible.
880 // - unlock the closure
881 // - update *q, we may have done *some* evaluation
882 // - if evac, we need to call evacuate(), because we
883 // need the write-barrier stuff.
884 // - undo the chain we've built to point to p.
885 SET_INFO((StgClosure *)p, (const StgInfoTable *)info_ptr);
886 *q = (StgClosure *)p;
887 if (evac) evacuate(q);
888 unchain_thunk_selectors(prev_thunk_selector, (StgClosure *)p);
889 return;
890 }
891 }
892 #else
893 // Save the real info pointer (NOTE: not the same as get_itbl()).
894 info_ptr = (StgWord)p->header.info;
895 SET_INFO((StgClosure *)p,&stg_WHITEHOLE_info);
896 #endif
897
898 field = INFO_PTR_TO_STRUCT((StgInfoTable *)info_ptr)->layout.selector_offset;
899
900 // The selectee might be a constructor closure,
901 // so we untag the pointer.
902 selectee = UNTAG_CLOSURE(p->selectee);
903
904 selector_loop:
905 // selectee now points to the closure that we're trying to select
906 // a field from. It may or may not be in to-space: we try not to
907 // end up in to-space, but it's impractical to avoid it in
908 // general. The compacting GC scatters to-space pointers in
909 // from-space during marking, for example. We rely on the property
910 // that evacuate() doesn't mind if it gets passed a to-space pointer.
911
912 info = (StgInfoTable*)selectee->header.info;
913
914 if (IS_FORWARDING_PTR(info)) {
915 // We don't follow pointers into to-space; the constructor
916 // has already been evacuated, so we won't save any space
917 // leaks by evaluating this selector thunk anyhow.
918 goto bale_out;
919 }
920
921 info = INFO_PTR_TO_STRUCT(info);
922 switch (info->type) {
923 case WHITEHOLE:
924 goto bale_out; // about to be evacuated by another thread (or a loop).
925
926 case CONSTR:
927 case CONSTR_1_0:
928 case CONSTR_0_1:
929 case CONSTR_2_0:
930 case CONSTR_1_1:
931 case CONSTR_0_2:
932 case CONSTR_STATIC:
933 case CONSTR_NOCAF_STATIC:
934 {
935 // check that the size is in range
936 ASSERT(field < (StgWord32)(info->layout.payload.ptrs +
937 info->layout.payload.nptrs));
938
939 // Select the right field from the constructor
940 val = selectee->payload[field];
941
942 #ifdef PROFILING
943 // For the purposes of LDV profiling, we have destroyed
944 // the original selector thunk, p.
945 SET_INFO((StgClosure*)p, (StgInfoTable *)info_ptr);
946 OVERWRITING_CLOSURE((StgClosure*)p);
947 SET_INFO((StgClosure*)p, &stg_WHITEHOLE_info);
948 #endif
949
950 // the closure in val is now the "value" of the
951 // THUNK_SELECTOR in p. However, val may itself be a
952 // THUNK_SELECTOR, in which case we want to continue
953 // evaluating until we find the real value, and then
954 // update the whole chain to point to the value.
955 val_loop:
956 info_ptr = (StgWord)UNTAG_CLOSURE(val)->header.info;
957 if (!IS_FORWARDING_PTR(info_ptr))
958 {
959 info = INFO_PTR_TO_STRUCT((StgInfoTable *)info_ptr);
960 switch (info->type) {
961 case IND:
962 case IND_PERM:
963 case IND_STATIC:
964 val = ((StgInd *)val)->indirectee;
965 goto val_loop;
966 case THUNK_SELECTOR:
967 ((StgClosure*)p)->payload[0] = (StgClosure *)prev_thunk_selector;
968 prev_thunk_selector = p;
969 p = (StgSelector*)val;
970 goto selector_chain;
971 default:
972 break;
973 }
974 }
975 ((StgClosure*)p)->payload[0] = (StgClosure *)prev_thunk_selector;
976 prev_thunk_selector = p;
977
978 *q = val;
979
980 // update the other selectors in the chain *before*
981 // evacuating the value. This is necessary in the case
982 // where the value turns out to be one of the selectors
983 // in the chain (i.e. we have a loop), and evacuating it
984 // would corrupt the chain.
985 unchain_thunk_selectors(prev_thunk_selector, val);
986
987 // evacuate() cannot recurse through
988 // eval_thunk_selector(), because we know val is not
989 // a THUNK_SELECTOR.
990 if (evac) evacuate(q);
991 return;
992 }
993
994 case IND:
995 case IND_PERM:
996 case IND_STATIC:
997 // Again, we might need to untag a constructor.
998 selectee = UNTAG_CLOSURE( ((StgInd *)selectee)->indirectee );
999 goto selector_loop;
1000
1001 case BLACKHOLE:
1002 {
1003 StgClosure *r;
1004 const StgInfoTable *i;
1005 r = ((StgInd*)selectee)->indirectee;
1006
1007 // establish whether this BH has been updated, and is now an
1008 // indirection, as in evacuate().
1009 if (GET_CLOSURE_TAG(r) == 0) {
1010 i = r->header.info;
1011 if (IS_FORWARDING_PTR(i)) {
1012 r = (StgClosure *)UN_FORWARDING_PTR(i);
1013 i = r->header.info;
1014 }
1015 if (i == &stg_TSO_info
1016 || i == &stg_WHITEHOLE_info
1017 || i == &stg_BLOCKING_QUEUE_CLEAN_info
1018 || i == &stg_BLOCKING_QUEUE_DIRTY_info) {
1019 goto bale_out;
1020 }
1021 ASSERT(i != &stg_IND_info);
1022 }
1023
1024 selectee = UNTAG_CLOSURE( ((StgInd *)selectee)->indirectee );
1025 goto selector_loop;
1026 }
1027
1028 case THUNK_SELECTOR:
1029 {
1030 StgClosure *val;
1031
1032 // recursively evaluate this selector. We don't want to
1033 // recurse indefinitely, so we impose a depth bound.
1034 if (gct->thunk_selector_depth >= MAX_THUNK_SELECTOR_DEPTH) {
1035 goto bale_out;
1036 }
1037
1038 gct->thunk_selector_depth++;
1039 // rtsFalse says "don't evacuate the result". It will,
1040 // however, update any THUNK_SELECTORs that are evaluated
1041 // along the way.
1042 eval_thunk_selector(&val, (StgSelector*)selectee, rtsFalse);
1043 gct->thunk_selector_depth--;
1044
1045 // did we actually manage to evaluate it?
1046 if (val == selectee) goto bale_out;
1047
1048 // Of course this pointer might be tagged...
1049 selectee = UNTAG_CLOSURE(val);
1050 goto selector_loop;
1051 }
1052
1053 case AP:
1054 case AP_STACK:
1055 case THUNK:
1056 case THUNK_1_0:
1057 case THUNK_0_1:
1058 case THUNK_2_0:
1059 case THUNK_1_1:
1060 case THUNK_0_2:
1061 case THUNK_STATIC:
1062 // not evaluated yet
1063 goto bale_out;
1064
1065 default:
1066 barf("eval_thunk_selector: strange selectee %d",
1067 (int)(info->type));
1068 }
1069
1070 bale_out:
1071 // We didn't manage to evaluate this thunk; restore the old info
1072 // pointer. But don't forget: we still need to evacuate the thunk itself.
1073 SET_INFO((StgClosure *)p, (const StgInfoTable *)info_ptr);
1074 // THREADED_RTS: we just unlocked the thunk, so another thread
1075 // might get in and update it. copy() will lock it again and
1076 // check whether it was updated in the meantime.
1077 *q = (StgClosure *)p;
1078 if (evac) {
1079 copy(q,(const StgInfoTable *)info_ptr,(StgClosure *)p,THUNK_SELECTOR_sizeW(),bd->dest_no);
1080 }
1081 unchain_thunk_selectors(prev_thunk_selector, *q);
1082 return;
1083 }