1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team 1998-2008
5 * Generational garbage collector: evacuation functions
7 * Documentation on the architecture of the Garbage Collector can be
8 * found in the online commentary:
10 * http://hackage.haskell.org/trac/ghc/wiki/Commentary/Rts/Storage/GC
12 * ---------------------------------------------------------------------------*/
23 #include "LdvProfile.h"
26 #if defined(PROF_SPIN) && defined(THREADED_RTS) && defined(PARALLEL_GC)
27 StgWord64 whitehole_spin = 0;
30 #if defined(THREADED_RTS) && !defined(PARALLEL_GC)
31 #define evacuate(p) evacuate1(p)
34 #if !defined(PARALLEL_GC)
35 #define copy_tag_nolock(p, info, src, size, stp, tag) \
36 copy_tag(p, info, src, size, stp, tag)
39 /* Used to avoid long recursion due to selector thunks
41 #define MAX_THUNK_SELECTOR_DEPTH 16
43 static void eval_thunk_selector (StgClosure **q, StgSelector * p, rtsBool);
44 STATIC_INLINE void evacuate_large(StgPtr p);
46 /* -----------------------------------------------------------------------------
47 Allocate some space in which to copy an object.
48 -------------------------------------------------------------------------- */
51 alloc_for_copy (nat size, step *stp)
56 /* Find out where we're going, using the handy "to" pointer in
57 * the step of the source object. If it turns out we need to
58 * evacuate to an older generation, adjust it here (see comment
61 if (stp < gct->evac_step) {
62 if (gct->eager_promotion) {
65 gct->failed_to_evac = rtsTrue;
69 ws = &gct->steps[stp->abs_no];
70 // this compiles to a single mem access to stp->abs_no only
72 /* chain a new block onto the to-space for the destination step if
76 if (to + size > ws->todo_lim) {
77 to = todo_block_full(size, ws);
79 ws->todo_free = to + size;
80 ASSERT(ws->todo_free >= ws->todo_bd->free && ws->todo_free <= ws->todo_lim);
85 /* -----------------------------------------------------------------------------
87 -------------------------------------------------------------------------- */
89 STATIC_INLINE GNUC_ATTR_HOT void
90 copy_tag(StgClosure **p, const StgInfoTable *info,
91 StgClosure *src, nat size, step *stp, StgWord tag)
96 to = alloc_for_copy(size,stp);
98 TICK_GC_WORDS_COPIED(size);
102 for (i = 1; i < size; i++) { // unroll for small i
106 // if (to+size+2 < bd->start + BLOCK_SIZE_W) {
107 // __builtin_prefetch(to + size + 2, 1);
110 #if defined(PARALLEL_GC)
112 const StgInfoTable *new_info;
113 new_info = (const StgInfoTable *)cas((StgPtr)&src->header.info, (W_)info, MK_FORWARDING_PTR(to));
114 if (new_info != info) {
115 return evacuate(p); // does the failed_to_evac stuff
117 *p = TAG_CLOSURE(tag,(StgClosure*)to);
121 src->header.info = (const StgInfoTable *)MK_FORWARDING_PTR(to);
122 *p = TAG_CLOSURE(tag,(StgClosure*)to);
126 // We store the size of the just evacuated object in the LDV word so that
127 // the profiler can guess the position of the next object later.
128 SET_EVACUAEE_FOR_LDV(from, size);
132 #if defined(PARALLEL_GC)
134 copy_tag_nolock(StgClosure **p, const StgInfoTable *info,
135 StgClosure *src, nat size, step *stp, StgWord tag)
140 to = alloc_for_copy(size,stp);
141 *p = TAG_CLOSURE(tag,(StgClosure*)to);
142 src->header.info = (const StgInfoTable *)MK_FORWARDING_PTR(to);
144 TICK_GC_WORDS_COPIED(size);
148 for (i = 1; i < size; i++) { // unroll for small i
152 // if (to+size+2 < bd->start + BLOCK_SIZE_W) {
153 // __builtin_prefetch(to + size + 2, 1);
157 // We store the size of the just evacuated object in the LDV word so that
158 // the profiler can guess the position of the next object later.
159 SET_EVACUAEE_FOR_LDV(from, size);
164 /* Special version of copy() for when we only want to copy the info
165 * pointer of an object, but reserve some padding after it. This is
166 * used to optimise evacuation of BLACKHOLEs.
169 copyPart(StgClosure **p, StgClosure *src, nat size_to_reserve, nat size_to_copy, step *stp)
175 #if defined(PARALLEL_GC)
177 info = xchg((StgPtr)&src->header.info, (W_)&stg_WHITEHOLE_info);
178 if (info == (W_)&stg_WHITEHOLE_info) {
184 if (IS_FORWARDING_PTR(info)) {
185 src->header.info = (const StgInfoTable *)info;
186 evacuate(p); // does the failed_to_evac stuff
190 info = (W_)src->header.info;
193 to = alloc_for_copy(size_to_reserve, stp);
194 *p = (StgClosure *)to;
196 TICK_GC_WORDS_COPIED(size_to_copy);
200 for (i = 1; i < size_to_copy; i++) { // unroll for small i
204 #if defined(PARALLEL_GC)
207 src->header.info = (const StgInfoTable*)MK_FORWARDING_PTR(to);
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);
214 if (size_to_reserve - size_to_copy > 0)
215 LDV_FILL_SLOP(to + size_to_copy, (int)(size_to_reserve - size_to_copy));
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, step *stp)
227 copy_tag(p,info,src,size,stp,0);
230 /* -----------------------------------------------------------------------------
231 Evacuate a large object
233 This just consists of removing the object from the (doubly-linked)
234 step->large_objects list, and linking it on to the (singly-linked)
235 step->new_large_objects list, from where it will be scavenged later.
237 Convention: bd->flags has BF_EVACUATED set for a large object
238 that has been evacuated, or unset otherwise.
239 -------------------------------------------------------------------------- */
242 evacuate_large(StgPtr p)
244 bdescr *bd = Bdescr(p);
249 ACQUIRE_SPIN_LOCK(&stp->sync_large_objects);
251 // already evacuated?
252 if (bd->flags & BF_EVACUATED) {
253 /* Don't forget to set the gct->failed_to_evac flag if we didn't get
254 * the desired destination (see comments in evacuate()).
256 if (stp < gct->evac_step) {
257 gct->failed_to_evac = rtsTrue;
258 TICK_GC_FAILED_PROMOTION();
260 RELEASE_SPIN_LOCK(&stp->sync_large_objects);
264 // remove from large_object list
266 bd->u.back->link = bd->link;
267 } else { // first object in the list
268 stp->large_objects = bd->link;
271 bd->link->u.back = bd->u.back;
274 /* link it on to the evacuated large object list of the destination step
277 if (new_stp < gct->evac_step) {
278 if (gct->eager_promotion) {
279 new_stp = gct->evac_step;
281 gct->failed_to_evac = rtsTrue;
285 ws = &gct->steps[new_stp->abs_no];
287 bd->flags |= BF_EVACUATED;
289 bd->gen_no = new_stp->gen_no;
291 // If this is a block of pinned objects, we don't have to scan
292 // these objects, because they aren't allowed to contain any
293 // pointers. For these blocks, we skip the scavenge stage and put
294 // them straight on the scavenged_large_objects list.
295 if (bd->flags & BF_PINNED) {
296 ASSERT(get_itbl((StgClosure *)p)->type == ARR_WORDS);
297 dbl_link_onto(bd, &ws->step->scavenged_large_objects);
298 ws->step->n_scavenged_large_blocks += bd->blocks;
300 bd->link = ws->todo_large_objects;
301 ws->todo_large_objects = bd;
304 RELEASE_SPIN_LOCK(&stp->sync_large_objects);
307 /* ----------------------------------------------------------------------------
310 This is called (eventually) for every live object in the system.
312 The caller to evacuate specifies a desired generation in the
313 gct->evac_step thread-local variable. The following conditions apply to
314 evacuating an object which resides in generation M when we're
315 collecting up to generation N
317 if M >= gct->evac_step
319 else evac to step->to
321 if M < gct->evac_step evac to gct->evac_step, step 0
323 if the object is already evacuated, then we check which generation
326 if M >= gct->evac_step do nothing
327 if M < gct->evac_step set gct->failed_to_evac flag to indicate that we
328 didn't manage to evacuate this object into gct->evac_step.
333 evacuate() is the single most important function performance-wise
334 in the GC. Various things have been tried to speed it up, but as
335 far as I can tell the code generated by gcc 3.2 with -O2 is about
336 as good as it's going to get. We pass the argument to evacuate()
337 in a register using the 'regparm' attribute (see the prototype for
338 evacuate() near the top of this file).
340 Changing evacuate() to take an (StgClosure **) rather than
341 returning the new pointer seems attractive, because we can avoid
342 writing back the pointer when it hasn't changed (eg. for a static
343 object, or an object in a generation > N). However, I tried it and
344 it doesn't help. One reason is that the (StgClosure **) pointer
345 gets spilled to the stack inside evacuate(), resulting in far more
346 extra reads/writes than we save.
347 ------------------------------------------------------------------------- */
349 REGPARM1 GNUC_ATTR_HOT void
350 evacuate(StgClosure **p)
355 const StgInfoTable *info;
361 /* The tag and the pointer are split, to be merged after evacing */
362 tag = GET_CLOSURE_TAG(q);
363 q = UNTAG_CLOSURE(q);
365 ASSERT(LOOKS_LIKE_CLOSURE_PTR(q));
367 if (!HEAP_ALLOCED(q)) {
369 if (!major_gc) return;
372 switch (info->type) {
375 if (info->srt_bitmap != 0) {
376 if (*THUNK_STATIC_LINK((StgClosure *)q) == NULL) {
378 *THUNK_STATIC_LINK((StgClosure *)q) = gct->static_objects;
379 gct->static_objects = (StgClosure *)q;
382 link = (StgPtr)cas((StgPtr)THUNK_STATIC_LINK((StgClosure *)q),
384 (StgWord)gct->static_objects);
386 gct->static_objects = (StgClosure *)q;
394 if (info->srt_bitmap != 0 &&
395 *FUN_STATIC_LINK((StgClosure *)q) == NULL) {
397 *FUN_STATIC_LINK((StgClosure *)q) = gct->static_objects;
398 gct->static_objects = (StgClosure *)q;
401 link = (StgPtr)cas((StgPtr)FUN_STATIC_LINK((StgClosure *)q),
403 (StgWord)gct->static_objects);
405 gct->static_objects = (StgClosure *)q;
412 /* If q->saved_info != NULL, then it's a revertible CAF - it'll be
413 * on the CAF list, so don't do anything with it here (we'll
414 * scavenge it later).
416 if (((StgIndStatic *)q)->saved_info == NULL) {
417 if (*IND_STATIC_LINK((StgClosure *)q) == NULL) {
419 *IND_STATIC_LINK((StgClosure *)q) = gct->static_objects;
420 gct->static_objects = (StgClosure *)q;
423 link = (StgPtr)cas((StgPtr)IND_STATIC_LINK((StgClosure *)q),
425 (StgWord)gct->static_objects);
427 gct->static_objects = (StgClosure *)q;
435 if (*STATIC_LINK(info,(StgClosure *)q) == NULL) {
437 *STATIC_LINK(info,(StgClosure *)q) = gct->static_objects;
438 gct->static_objects = (StgClosure *)q;
441 link = (StgPtr)cas((StgPtr)STATIC_LINK(info,(StgClosure *)q),
443 (StgWord)gct->static_objects);
445 gct->static_objects = (StgClosure *)q;
449 /* I am assuming that static_objects pointers are not
450 * written to other objects, and thus, no need to retag. */
453 case CONSTR_NOCAF_STATIC:
454 /* no need to put these on the static linked list, they don't need
460 barf("evacuate(static): strange closure type %d", (int)(info->type));
466 if ((bd->flags & (BF_LARGE | BF_MARKED | BF_EVACUATED)) != 0) {
468 // pointer into to-space: just return it. It might be a pointer
469 // into a generation that we aren't collecting (> N), or it
470 // might just be a pointer into to-space. The latter doesn't
471 // happen often, but allowing it makes certain things a bit
472 // easier; e.g. scavenging an object is idempotent, so it's OK to
473 // have an object on the mutable list multiple times.
474 if (bd->flags & BF_EVACUATED) {
475 // We aren't copying this object, so we have to check
476 // whether it is already in the target generation. (this is
477 // the write barrier).
478 if (bd->step < gct->evac_step) {
479 gct->failed_to_evac = rtsTrue;
480 TICK_GC_FAILED_PROMOTION();
485 /* evacuate large objects by re-linking them onto a different list.
487 if (bd->flags & BF_LARGE) {
489 if (info->type == TSO &&
490 ((StgTSO *)q)->what_next == ThreadRelocated) {
491 q = (StgClosure *)((StgTSO *)q)->_link;
495 evacuate_large((P_)q);
499 /* If the object is in a step that we're compacting, then we
500 * need to use an alternative evacuate procedure.
502 if (!is_marked((P_)q,bd)) {
504 if (mark_stack_full()) {
505 debugTrace(DEBUG_gc,"mark stack overflowed");
506 mark_stack_overflowed = rtsTrue;
509 push_mark_stack((P_)q);
516 info = q->header.info;
517 if (IS_FORWARDING_PTR(info))
519 /* Already evacuated, just return the forwarding address.
520 * HOWEVER: if the requested destination generation (gct->evac_step) is
521 * older than the actual generation (because the object was
522 * already evacuated to a younger generation) then we have to
523 * set the gct->failed_to_evac flag to indicate that we couldn't
524 * manage to promote the object to the desired generation.
527 * Optimisation: the check is fairly expensive, but we can often
528 * shortcut it if either the required generation is 0, or the
529 * current object (the EVACUATED) is in a high enough generation.
530 * We know that an EVACUATED always points to an object in the
531 * same or an older generation. stp is the lowest step that the
532 * current object would be evacuated to, so we only do the full
533 * check if stp is too low.
535 StgClosure *e = (StgClosure*)UN_FORWARDING_PTR(info);
536 *p = TAG_CLOSURE(tag,e);
537 if (stp < gct->evac_step) { // optimisation
538 if (Bdescr((P_)e)->step < gct->evac_step) {
539 gct->failed_to_evac = rtsTrue;
540 TICK_GC_FAILED_PROMOTION();
546 switch (INFO_PTR_TO_STRUCT(info)->type) {
555 copy(p,info,q,sizeW_fromITBL(INFO_PTR_TO_STRUCT(info)),stp);
560 StgWord w = (StgWord)q->payload[0];
561 if (info == Czh_con_info &&
562 // unsigned, so always true: (StgChar)w >= MIN_CHARLIKE &&
563 (StgChar)w <= MAX_CHARLIKE) {
564 *p = TAG_CLOSURE(tag,
565 (StgClosure *)CHARLIKE_CLOSURE((StgChar)w)
568 else if (info == Izh_con_info &&
569 (StgInt)w >= MIN_INTLIKE && (StgInt)w <= MAX_INTLIKE) {
570 *p = TAG_CLOSURE(tag,
571 (StgClosure *)INTLIKE_CLOSURE((StgInt)w)
575 copy_tag_nolock(p,info,q,sizeofW(StgHeader)+1,stp,tag);
583 copy_tag_nolock(p,info,q,sizeofW(StgHeader)+1,stp,tag);
588 copy(p,info,q,sizeofW(StgThunk)+1,stp);
594 #ifdef NO_PROMOTE_THUNKS
595 if (bd->gen_no == 0 &&
597 bd->step->no == generations[bd->gen_no].n_steps-1) {
601 copy(p,info,q,sizeofW(StgThunk)+2,stp);
609 copy_tag_nolock(p,info,q,sizeofW(StgHeader)+2,stp,tag);
613 copy_tag_nolock(p,info,q,sizeofW(StgHeader)+2,stp,tag);
617 copy(p,info,q,thunk_sizeW_fromITBL(INFO_PTR_TO_STRUCT(info)),stp);
622 case IND_OLDGEN_PERM:
624 copy_tag_nolock(p,info,q,sizeW_fromITBL(INFO_PTR_TO_STRUCT(info)),stp,tag);
629 copy_tag(p,info,q,sizeW_fromITBL(INFO_PTR_TO_STRUCT(info)),stp,tag);
633 copy(p,info,q,bco_sizeW((StgBCO *)q),stp);
638 copyPart(p,q,BLACKHOLE_sizeW(),sizeofW(StgHeader),stp);
642 eval_thunk_selector(p, (StgSelector *)q, rtsTrue);
647 // follow chains of indirections, don't evacuate them
648 q = ((StgInd*)q)->indirectee;
659 case CATCH_STM_FRAME:
660 case CATCH_RETRY_FRAME:
661 case ATOMICALLY_FRAME:
662 // shouldn't see these
663 barf("evacuate: stack frame at %p\n", q);
666 copy(p,info,q,pap_sizeW((StgPAP*)q),stp);
670 copy(p,info,q,ap_sizeW((StgAP*)q),stp);
674 copy(p,info,q,ap_stack_sizeW((StgAP_STACK*)q),stp);
678 // just copy the block
679 copy(p,info,q,arr_words_sizeW((StgArrWords *)q),stp);
682 case MUT_ARR_PTRS_CLEAN:
683 case MUT_ARR_PTRS_DIRTY:
684 case MUT_ARR_PTRS_FROZEN:
685 case MUT_ARR_PTRS_FROZEN0:
686 // just copy the block
687 copy(p,info,q,mut_arr_ptrs_sizeW((StgMutArrPtrs *)q),stp);
692 StgTSO *tso = (StgTSO *)q;
694 /* Deal with redirected TSOs (a TSO that's had its stack enlarged).
696 if (tso->what_next == ThreadRelocated) {
697 q = (StgClosure *)tso->_link;
702 /* To evacuate a small TSO, we need to relocate the update frame
710 mine = copyPart(p,(StgClosure *)tso, tso_sizeW(tso),
711 sizeofW(StgTSO), stp);
713 new_tso = (StgTSO *)*p;
714 move_TSO(tso, new_tso);
715 for (r = tso->sp, s = new_tso->sp;
716 r < tso->stack+tso->stack_size;) {
725 copy(p,info,q,sizeofW(StgTRecHeader),stp);
728 case TVAR_WATCH_QUEUE:
729 copy(p,info,q,sizeofW(StgTVarWatchQueue),stp);
733 copy(p,info,q,sizeofW(StgTVar),stp);
737 copy(p,info,q,sizeofW(StgTRecChunk),stp);
740 case ATOMIC_INVARIANT:
741 copy(p,info,q,sizeofW(StgAtomicInvariant),stp);
744 case INVARIANT_CHECK_QUEUE:
745 copy(p,info,q,sizeofW(StgInvariantCheckQueue),stp);
749 barf("evacuate: strange closure type %d", (int)(INFO_PTR_TO_STRUCT(info)->type));
755 /* -----------------------------------------------------------------------------
756 Evaluate a THUNK_SELECTOR if possible.
758 p points to a THUNK_SELECTOR that we want to evaluate. The
759 result of "evaluating" it will be evacuated and a pointer to the
760 to-space closure will be returned.
762 If the THUNK_SELECTOR could not be evaluated (its selectee is still
763 a THUNK, for example), then the THUNK_SELECTOR itself will be
765 -------------------------------------------------------------------------- */
767 unchain_thunk_selectors(StgSelector *p, StgClosure *val)
775 ASSERT(p->header.info == &stg_WHITEHOLE_info);
777 ASSERT(p->header.info == &stg_BLACKHOLE_info);
779 // val must be in to-space. Not always: when we recursively
780 // invoke eval_thunk_selector(), the recursive calls will not
781 // evacuate the value (because we want to select on the value,
782 // not evacuate it), so in this case val is in from-space.
783 // ASSERT(!HEAP_ALLOCED(val) || Bdescr((P_)val)->gen_no > N || (Bdescr((P_)val)->flags & BF_EVACUATED));
785 prev = (StgSelector*)((StgClosure *)p)->payload[0];
787 // Update the THUNK_SELECTOR with an indirection to the
788 // value. The value is still in from-space at this stage.
790 // (old note: Why not do upd_evacuee(q,p)? Because we have an
791 // invariant that an EVACUATED closure always points to an
792 // object in the same or an older generation (required by
793 // the short-cut test in the EVACUATED case, below).
794 if ((StgClosure *)p == val) {
795 // must be a loop; just leave a BLACKHOLE in place. This
796 // can happen when we have a chain of selectors that
797 // eventually loops back on itself. We can't leave an
798 // indirection pointing to itself, and we want the program
799 // to deadlock if it ever enters this closure, so
800 // BLACKHOLE is correct.
801 SET_INFO(p, &stg_BLACKHOLE_info);
803 ((StgInd *)p)->indirectee = val;
805 SET_INFO(p, &stg_IND_info);
808 // For the purposes of LDV profiling, we have created an
810 LDV_RECORD_CREATE(p);
817 eval_thunk_selector (StgClosure **q, StgSelector * p, rtsBool evac)
818 // NB. for legacy reasons, p & q are swapped around :(
823 StgClosure *selectee;
824 StgSelector *prev_thunk_selector;
828 prev_thunk_selector = NULL;
829 // this is a chain of THUNK_SELECTORs that we are going to update
830 // to point to the value of the current THUNK_SELECTOR. Each
831 // closure on the chain is a BLACKHOLE, and points to the next in the
832 // chain with payload[0].
836 bd = Bdescr((StgPtr)p);
837 if (HEAP_ALLOCED(p)) {
838 // If the THUNK_SELECTOR is in to-space or in a generation that we
839 // are not collecting, then bale out early. We won't be able to
840 // save any space in any case, and updating with an indirection is
841 // trickier in a non-collected gen: we would have to update the
843 if (bd->flags & BF_EVACUATED) {
844 unchain_thunk_selectors(prev_thunk_selector, (StgClosure *)p);
845 *q = (StgClosure *)p;
846 // shortcut, behave as for: if (evac) evacuate(q);
847 if (evac && bd->step < gct->evac_step) {
848 gct->failed_to_evac = rtsTrue;
849 TICK_GC_FAILED_PROMOTION();
853 // we don't update THUNK_SELECTORS in the compacted
854 // generation, because compaction does not remove the INDs
855 // that result, this causes confusion later
856 // (scavenge_mark_stack doesn't deal with IND). BEWARE! This
857 // bit is very tricky to get right. If you make changes
858 // around here, test by compiling stage 3 with +RTS -c -RTS.
859 if (bd->flags & BF_MARKED) {
860 // must call evacuate() to mark this closure if evac==rtsTrue
861 *q = (StgClosure *)p;
862 if (evac) evacuate(q);
863 unchain_thunk_selectors(prev_thunk_selector, (StgClosure *)p);
869 // BLACKHOLE the selector thunk, since it is now under evaluation.
870 // This is important to stop us going into an infinite loop if
871 // this selector thunk eventually refers to itself.
872 #if defined(THREADED_RTS)
873 // In threaded mode, we'll use WHITEHOLE to lock the selector
874 // thunk while we evaluate it.
877 info_ptr = xchg((StgPtr)&p->header.info, (W_)&stg_WHITEHOLE_info);
878 } while (info_ptr == (W_)&stg_WHITEHOLE_info);
880 // make sure someone else didn't get here first...
881 if (IS_FORWARDING_PTR(p) ||
882 INFO_PTR_TO_STRUCT(info_ptr)->type != THUNK_SELECTOR) {
883 // v. tricky now. The THUNK_SELECTOR has been evacuated
884 // by another thread, and is now either a forwarding ptr or IND.
885 // We need to extract ourselves from the current situation
886 // as cleanly as possible.
887 // - unlock the closure
888 // - update *q, we may have done *some* evaluation
889 // - if evac, we need to call evacuate(), because we
890 // need the write-barrier stuff.
891 // - undo the chain we've built to point to p.
892 SET_INFO(p, (const StgInfoTable *)info_ptr);
893 *q = (StgClosure *)p;
894 if (evac) evacuate(q);
895 unchain_thunk_selectors(prev_thunk_selector, (StgClosure *)p);
900 // Save the real info pointer (NOTE: not the same as get_itbl()).
901 info_ptr = (StgWord)p->header.info;
902 SET_INFO(p,&stg_BLACKHOLE_info);
905 field = INFO_PTR_TO_STRUCT(info_ptr)->layout.selector_offset;
907 // The selectee might be a constructor closure,
908 // so we untag the pointer.
909 selectee = UNTAG_CLOSURE(p->selectee);
912 // selectee now points to the closure that we're trying to select
913 // a field from. It may or may not be in to-space: we try not to
914 // end up in to-space, but it's impractical to avoid it in
915 // general. The compacting GC scatters to-space pointers in
916 // from-space during marking, for example. We rely on the property
917 // that evacuate() doesn't mind if it gets passed a to-space pointer.
919 info = (StgInfoTable*)selectee->header.info;
921 if (IS_FORWARDING_PTR(info)) {
922 // We don't follow pointers into to-space; the constructor
923 // has already been evacuated, so we won't save any space
924 // leaks by evaluating this selector thunk anyhow.
928 info = INFO_PTR_TO_STRUCT(info);
929 switch (info->type) {
931 goto bale_out; // about to be evacuated by another thread (or a loop).
940 case CONSTR_NOCAF_STATIC:
942 // check that the size is in range
943 ASSERT(field < (StgWord32)(info->layout.payload.ptrs +
944 info->layout.payload.nptrs));
946 // Select the right field from the constructor
947 val = selectee->payload[field];
950 // For the purposes of LDV profiling, we have destroyed
951 // the original selector thunk, p.
952 SET_INFO(p, (StgInfoTable *)info_ptr);
953 LDV_RECORD_DEAD_FILL_SLOP_DYNAMIC((StgClosure *)p);
954 #if defined(THREADED_RTS)
955 SET_INFO(p, &stg_WHITEHOLE_info);
957 SET_INFO(p, &stg_BLACKHOLE_info);
961 // the closure in val is now the "value" of the
962 // THUNK_SELECTOR in p. However, val may itself be a
963 // THUNK_SELECTOR, in which case we want to continue
964 // evaluating until we find the real value, and then
965 // update the whole chain to point to the value.
967 info_ptr = (StgWord)UNTAG_CLOSURE(val)->header.info;
968 if (!IS_FORWARDING_PTR(info_ptr))
970 info = INFO_PTR_TO_STRUCT(info_ptr);
971 switch (info->type) {
975 case IND_OLDGEN_PERM:
977 val = ((StgInd *)val)->indirectee;
980 ((StgClosure*)p)->payload[0] = (StgClosure *)prev_thunk_selector;
981 prev_thunk_selector = p;
982 p = (StgSelector*)val;
988 ((StgClosure*)p)->payload[0] = (StgClosure *)prev_thunk_selector;
989 prev_thunk_selector = p;
993 // update the other selectors in the chain *before*
994 // evacuating the value. This is necessary in the case
995 // where the value turns out to be one of the selectors
996 // in the chain (i.e. we have a loop), and evacuating it
997 // would corrupt the chain.
998 unchain_thunk_selectors(prev_thunk_selector, val);
1000 // evacuate() cannot recurse through
1001 // eval_thunk_selector(), because we know val is not
1002 // a THUNK_SELECTOR.
1003 if (evac) evacuate(q);
1010 case IND_OLDGEN_PERM:
1012 // Again, we might need to untag a constructor.
1013 selectee = UNTAG_CLOSURE( ((StgInd *)selectee)->indirectee );
1016 case THUNK_SELECTOR:
1020 // recursively evaluate this selector. We don't want to
1021 // recurse indefinitely, so we impose a depth bound.
1022 if (gct->thunk_selector_depth >= MAX_THUNK_SELECTOR_DEPTH) {
1026 gct->thunk_selector_depth++;
1027 // rtsFalse says "don't evacuate the result". It will,
1028 // however, update any THUNK_SELECTORs that are evaluated
1030 eval_thunk_selector(&val, (StgSelector*)selectee, rtsFalse);
1031 gct->thunk_selector_depth--;
1033 // did we actually manage to evaluate it?
1034 if (val == selectee) goto bale_out;
1036 // Of course this pointer might be tagged...
1037 selectee = UNTAG_CLOSURE(val);
1052 // not evaluated yet
1056 barf("eval_thunk_selector: strange selectee %d",
1061 // We didn't manage to evaluate this thunk; restore the old info
1062 // pointer. But don't forget: we still need to evacuate the thunk itself.
1063 SET_INFO(p, (const StgInfoTable *)info_ptr);
1064 // THREADED_RTS: we just unlocked the thunk, so another thread
1065 // might get in and update it. copy() will lock it again and
1066 // check whether it was updated in the meantime.
1067 *q = (StgClosure *)p;
1069 copy(q,(const StgInfoTable *)info_ptr,(StgClosure *)p,THUNK_SELECTOR_sizeW(),bd->step->to);
1071 unchain_thunk_selectors(prev_thunk_selector, *q);