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 * ---------------------------------------------------------------------------*/
14 #include "PosixSource.h"
23 #include "MarkStack.h"
26 #include "LdvProfile.h"
28 #if defined(PROF_SPIN) && defined(THREADED_RTS) && defined(PARALLEL_GC)
29 StgWord64 whitehole_spin = 0;
32 #if defined(THREADED_RTS) && !defined(PARALLEL_GC)
33 #define evacuate(p) evacuate1(p)
34 #define HEAP_ALLOCED_GC(p) HEAP_ALLOCED(p)
37 #if !defined(PARALLEL_GC)
38 #define copy_tag_nolock(p, info, src, size, stp, tag) \
39 copy_tag(p, info, src, size, stp, tag)
42 /* Used to avoid long recursion due to selector thunks
44 #define MAX_THUNK_SELECTOR_DEPTH 16
46 static void eval_thunk_selector (StgClosure **q, StgSelector * p, rtsBool);
47 STATIC_INLINE void evacuate_large(StgPtr p);
49 /* -----------------------------------------------------------------------------
50 Allocate some space in which to copy an object.
51 -------------------------------------------------------------------------- */
54 alloc_for_copy (nat size, nat gen_no)
59 /* Find out where we're going, using the handy "to" pointer in
60 * the gen of the source object. If it turns out we need to
61 * evacuate to an older generation, adjust it here (see comment
64 if (gen_no < gct->evac_gen_no) {
65 if (gct->eager_promotion) {
66 gen_no = gct->evac_gen_no;
68 gct->failed_to_evac = rtsTrue;
72 ws = &gct->gens[gen_no]; // zero memory references here
74 /* chain a new block onto the to-space for the destination gen if
78 ws->todo_free += size;
79 if (ws->todo_free > ws->todo_lim) {
80 to = todo_block_full(size, ws);
82 ASSERT(ws->todo_free >= ws->todo_bd->free && ws->todo_free <= ws->todo_lim);
87 /* -----------------------------------------------------------------------------
89 -------------------------------------------------------------------------- */
91 STATIC_INLINE GNUC_ATTR_HOT void
92 copy_tag(StgClosure **p, const StgInfoTable *info,
93 StgClosure *src, nat size, nat gen_no, StgWord tag)
98 to = alloc_for_copy(size,gen_no);
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, nat gen_no, StgWord tag)
140 to = alloc_for_copy(size,gen_no);
144 for (i = 1; i < size; i++) { // unroll for small i
148 // if somebody else reads the forwarding pointer, we better make
149 // sure there's a closure at the end of it.
151 *p = TAG_CLOSURE(tag,(StgClosure*)to);
152 src->header.info = (const StgInfoTable *)MK_FORWARDING_PTR(to);
154 // if (to+size+2 < bd->start + BLOCK_SIZE_W) {
155 // __builtin_prefetch(to + size + 2, 1);
159 // We store the size of the just evacuated object in the LDV word so that
160 // the profiler can guess the position of the next object later.
161 SET_EVACUAEE_FOR_LDV(from, size);
166 /* Special version of copy() for when we only want to copy the info
167 * pointer of an object, but reserve some padding after it. This is
168 * used to optimise evacuation of TSOs.
171 copyPart(StgClosure **p, StgClosure *src, nat size_to_reserve,
172 nat size_to_copy, nat gen_no)
178 #if defined(PARALLEL_GC)
180 info = xchg((StgPtr)&src->header.info, (W_)&stg_WHITEHOLE_info);
181 if (info == (W_)&stg_WHITEHOLE_info) {
187 if (IS_FORWARDING_PTR(info)) {
188 src->header.info = (const StgInfoTable *)info;
189 evacuate(p); // does the failed_to_evac stuff
193 info = (W_)src->header.info;
196 to = alloc_for_copy(size_to_reserve, gen_no);
200 for (i = 1; i < size_to_copy; i++) { // unroll for small i
205 src->header.info = (const StgInfoTable*)MK_FORWARDING_PTR(to);
206 *p = (StgClosure *)to;
209 // We store the size of the just evacuated object in the LDV word so that
210 // the profiler can guess the position of the next object later.
211 SET_EVACUAEE_FOR_LDV(from, size_to_reserve);
213 if (size_to_reserve - size_to_copy > 0)
214 LDV_FILL_SLOP(to + size_to_copy, (int)(size_to_reserve - size_to_copy));
221 /* Copy wrappers that don't tag the closure after copying */
222 STATIC_INLINE GNUC_ATTR_HOT void
223 copy(StgClosure **p, const StgInfoTable *info,
224 StgClosure *src, nat size, nat gen_no)
226 copy_tag(p,info,src,size,gen_no,0);
229 /* -----------------------------------------------------------------------------
230 Evacuate a large object
232 This just consists of removing the object from the (doubly-linked)
233 gen->large_objects list, and linking it on to the (singly-linked)
234 gen->new_large_objects list, from where it will be scavenged later.
236 Convention: bd->flags has BF_EVACUATED set for a large object
237 that has been evacuated, or unset otherwise.
238 -------------------------------------------------------------------------- */
241 evacuate_large(StgPtr p)
244 generation *gen, *new_gen;
245 nat gen_no, new_gen_no;
251 ACQUIRE_SPIN_LOCK(&gen->sync);
253 // already evacuated?
254 if (bd->flags & BF_EVACUATED) {
255 /* Don't forget to set the gct->failed_to_evac flag if we didn't get
256 * the desired destination (see comments in evacuate()).
258 if (gen_no < gct->evac_gen_no) {
259 gct->failed_to_evac = rtsTrue;
260 TICK_GC_FAILED_PROMOTION();
262 RELEASE_SPIN_LOCK(&gen->sync);
266 // remove from large_object list
268 bd->u.back->link = bd->link;
269 } else { // first object in the list
270 gen->large_objects = bd->link;
273 bd->link->u.back = bd->u.back;
276 /* link it on to the evacuated large object list of the destination gen
278 new_gen_no = bd->dest_no;
280 if (new_gen_no < gct->evac_gen_no) {
281 if (gct->eager_promotion) {
282 new_gen_no = gct->evac_gen_no;
284 gct->failed_to_evac = rtsTrue;
288 ws = &gct->gens[new_gen_no];
289 new_gen = &generations[new_gen_no];
291 bd->flags |= BF_EVACUATED;
292 initBdescr(bd, new_gen, new_gen->to);
294 // If this is a block of pinned objects, we don't have to scan
295 // these objects, because they aren't allowed to contain any
296 // pointers. For these blocks, we skip the scavenge stage and put
297 // them straight on the scavenged_large_objects list.
298 if (bd->flags & BF_PINNED) {
299 ASSERT(get_itbl((StgClosure *)p)->type == ARR_WORDS);
300 if (new_gen != gen) { ACQUIRE_SPIN_LOCK(&new_gen->sync); }
301 dbl_link_onto(bd, &new_gen->scavenged_large_objects);
302 new_gen->n_scavenged_large_blocks += bd->blocks;
303 if (new_gen != gen) { RELEASE_SPIN_LOCK(&new_gen->sync); }
305 bd->link = ws->todo_large_objects;
306 ws->todo_large_objects = bd;
309 RELEASE_SPIN_LOCK(&gen->sync);
312 /* ----------------------------------------------------------------------------
315 This is called (eventually) for every live object in the system.
317 The caller to evacuate specifies a desired generation in the
318 gct->evac_gen thread-local variable. The following conditions apply to
319 evacuating an object which resides in generation M when we're
320 collecting up to generation N
322 if M >= gct->evac_gen
326 if M < gct->evac_gen evac to gct->evac_gen, step 0
328 if the object is already evacuated, then we check which generation
331 if M >= gct->evac_gen do nothing
332 if M < gct->evac_gen set gct->failed_to_evac flag to indicate that we
333 didn't manage to evacuate this object into gct->evac_gen.
338 evacuate() is the single most important function performance-wise
339 in the GC. Various things have been tried to speed it up, but as
340 far as I can tell the code generated by gcc 3.2 with -O2 is about
341 as good as it's going to get. We pass the argument to evacuate()
342 in a register using the 'regparm' attribute (see the prototype for
343 evacuate() near the top of this file).
345 Changing evacuate() to take an (StgClosure **) rather than
346 returning the new pointer seems attractive, because we can avoid
347 writing back the pointer when it hasn't changed (eg. for a static
348 object, or an object in a generation > N). However, I tried it and
349 it doesn't help. One reason is that the (StgClosure **) pointer
350 gets spilled to the stack inside evacuate(), resulting in far more
351 extra reads/writes than we save.
352 ------------------------------------------------------------------------- */
354 REGPARM1 GNUC_ATTR_HOT void
355 evacuate(StgClosure **p)
360 const StgInfoTable *info;
366 /* The tag and the pointer are split, to be merged after evacing */
367 tag = GET_CLOSURE_TAG(q);
368 q = UNTAG_CLOSURE(q);
370 ASSERTM(LOOKS_LIKE_CLOSURE_PTR(q), "invalid closure, info=%p", q->header.info);
372 if (!HEAP_ALLOCED_GC(q)) {
374 if (!major_gc) return;
377 switch (info->type) {
380 if (info->srt_bitmap != 0) {
381 if (*THUNK_STATIC_LINK((StgClosure *)q) == NULL) {
383 *THUNK_STATIC_LINK((StgClosure *)q) = gct->static_objects;
384 gct->static_objects = (StgClosure *)q;
387 link = (StgPtr)cas((StgPtr)THUNK_STATIC_LINK((StgClosure *)q),
389 (StgWord)gct->static_objects);
391 gct->static_objects = (StgClosure *)q;
399 if (info->srt_bitmap != 0 &&
400 *FUN_STATIC_LINK((StgClosure *)q) == NULL) {
402 *FUN_STATIC_LINK((StgClosure *)q) = gct->static_objects;
403 gct->static_objects = (StgClosure *)q;
406 link = (StgPtr)cas((StgPtr)FUN_STATIC_LINK((StgClosure *)q),
408 (StgWord)gct->static_objects);
410 gct->static_objects = (StgClosure *)q;
417 /* If q->saved_info != NULL, then it's a revertible CAF - it'll be
418 * on the CAF list, so don't do anything with it here (we'll
419 * scavenge it later).
421 if (*IND_STATIC_LINK((StgClosure *)q) == NULL) {
423 *IND_STATIC_LINK((StgClosure *)q) = gct->static_objects;
424 gct->static_objects = (StgClosure *)q;
427 link = (StgPtr)cas((StgPtr)IND_STATIC_LINK((StgClosure *)q),
429 (StgWord)gct->static_objects);
431 gct->static_objects = (StgClosure *)q;
438 if (*STATIC_LINK(info,(StgClosure *)q) == NULL) {
440 *STATIC_LINK(info,(StgClosure *)q) = gct->static_objects;
441 gct->static_objects = (StgClosure *)q;
444 link = (StgPtr)cas((StgPtr)STATIC_LINK(info,(StgClosure *)q),
446 (StgWord)gct->static_objects);
448 gct->static_objects = (StgClosure *)q;
452 /* I am assuming that static_objects pointers are not
453 * written to other objects, and thus, no need to retag. */
456 case CONSTR_NOCAF_STATIC:
457 /* no need to put these on the static linked list, they don't need
463 barf("evacuate(static): strange closure type %d", (int)(info->type));
469 if ((bd->flags & (BF_LARGE | BF_MARKED | BF_EVACUATED)) != 0) {
471 // pointer into to-space: just return it. It might be a pointer
472 // into a generation that we aren't collecting (> N), or it
473 // might just be a pointer into to-space. The latter doesn't
474 // happen often, but allowing it makes certain things a bit
475 // easier; e.g. scavenging an object is idempotent, so it's OK to
476 // have an object on the mutable list multiple times.
477 if (bd->flags & BF_EVACUATED) {
478 // We aren't copying this object, so we have to check
479 // whether it is already in the target generation. (this is
480 // the write barrier).
481 if (bd->gen_no < gct->evac_gen_no) {
482 gct->failed_to_evac = rtsTrue;
483 TICK_GC_FAILED_PROMOTION();
488 /* evacuate large objects by re-linking them onto a different list.
490 if (bd->flags & BF_LARGE) {
491 evacuate_large((P_)q);
495 /* If the object is in a gen that we're compacting, then we
496 * need to use an alternative evacuate procedure.
498 if (!is_marked((P_)q,bd)) {
500 push_mark_stack((P_)q);
505 gen_no = bd->dest_no;
507 info = q->header.info;
508 if (IS_FORWARDING_PTR(info))
510 /* Already evacuated, just return the forwarding address.
511 * HOWEVER: if the requested destination generation (gct->evac_gen) is
512 * older than the actual generation (because the object was
513 * already evacuated to a younger generation) then we have to
514 * set the gct->failed_to_evac flag to indicate that we couldn't
515 * manage to promote the object to the desired generation.
518 * Optimisation: the check is fairly expensive, but we can often
519 * shortcut it if either the required generation is 0, or the
520 * current object (the EVACUATED) is in a high enough generation.
521 * We know that an EVACUATED always points to an object in the
522 * same or an older generation. gen is the lowest generation that the
523 * current object would be evacuated to, so we only do the full
524 * check if gen is too low.
526 StgClosure *e = (StgClosure*)UN_FORWARDING_PTR(info);
527 *p = TAG_CLOSURE(tag,e);
528 if (gen_no < gct->evac_gen_no) { // optimisation
529 if (Bdescr((P_)e)->gen_no < gct->evac_gen_no) {
530 gct->failed_to_evac = rtsTrue;
531 TICK_GC_FAILED_PROMOTION();
537 switch (INFO_PTR_TO_STRUCT(info)->type) {
546 copy(p,info,q,sizeW_fromITBL(INFO_PTR_TO_STRUCT(info)),gen_no);
549 // For ints and chars of low value, save space by replacing references to
550 // these with closures with references to common, shared ones in the RTS.
552 // * Except when compiling into Windows DLLs which don't support cross-package
553 // data references very well.
557 #if defined(__PIC__) && defined(mingw32_HOST_OS)
558 copy_tag_nolock(p,info,q,sizeofW(StgHeader)+1,gen_no,tag);
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,gen_no,tag);
584 copy_tag_nolock(p,info,q,sizeofW(StgHeader)+1,gen_no,tag);
589 copy(p,info,q,sizeofW(StgThunk)+1,gen_no);
595 #ifdef NO_PROMOTE_THUNKS
598 copy(p,info,q,sizeofW(StgThunk)+2,gen_no);
606 copy_tag_nolock(p,info,q,sizeofW(StgHeader)+2,gen_no,tag);
610 copy_tag_nolock(p,info,q,sizeofW(StgHeader)+2,gen_no,tag);
614 copy(p,info,q,thunk_sizeW_fromITBL(INFO_PTR_TO_STRUCT(info)),gen_no);
620 copy_tag_nolock(p,info,q,sizeW_fromITBL(INFO_PTR_TO_STRUCT(info)),gen_no,tag);
626 const StgInfoTable *i;
627 r = ((StgInd*)q)->indirectee;
628 if (GET_CLOSURE_TAG(r) == 0) {
630 if (IS_FORWARDING_PTR(i)) {
631 r = (StgClosure *)UN_FORWARDING_PTR(i);
634 if (i == &stg_TSO_info
635 || i == &stg_WHITEHOLE_info
636 || i == &stg_BLOCKING_QUEUE_CLEAN_info
637 || i == &stg_BLOCKING_QUEUE_DIRTY_info) {
638 copy(p,info,q,sizeofW(StgInd),gen_no);
641 ASSERT(i != &stg_IND_info);
652 copy(p,info,q,sizeW_fromITBL(INFO_PTR_TO_STRUCT(info)),gen_no);
656 copy(p,info,q,bco_sizeW((StgBCO *)q),gen_no);
660 eval_thunk_selector(p, (StgSelector *)q, rtsTrue);
664 // follow chains of indirections, don't evacuate them
665 q = ((StgInd*)q)->indirectee;
674 case UNDERFLOW_FRAME:
677 case CATCH_STM_FRAME:
678 case CATCH_RETRY_FRAME:
679 case ATOMICALLY_FRAME:
680 // shouldn't see these
681 barf("evacuate: stack frame at %p\n", q);
684 copy(p,info,q,pap_sizeW((StgPAP*)q),gen_no);
688 copy(p,info,q,ap_sizeW((StgAP*)q),gen_no);
692 copy(p,info,q,ap_stack_sizeW((StgAP_STACK*)q),gen_no);
696 // just copy the block
697 copy(p,info,q,arr_words_sizeW((StgArrWords *)q),gen_no);
700 case MUT_ARR_PTRS_CLEAN:
701 case MUT_ARR_PTRS_DIRTY:
702 case MUT_ARR_PTRS_FROZEN:
703 case MUT_ARR_PTRS_FROZEN0:
704 // just copy the block
705 copy(p,info,q,mut_arr_ptrs_sizeW((StgMutArrPtrs *)q),gen_no);
709 copy(p,info,q,sizeofW(StgTSO),gen_no);
714 StgStack *stack = (StgStack *)q;
716 /* To evacuate a small STACK, we need to adjust the stack pointer
723 mine = copyPart(p,(StgClosure *)stack, stack_sizeW(stack),
724 sizeofW(StgStack), gen_no);
726 new_stack = (StgStack *)*p;
727 move_STACK(stack, new_stack);
728 for (r = stack->sp, s = new_stack->sp;
729 r < stack->stack + stack->stack_size;) {
738 copy(p,info,q,sizeofW(StgTRecChunk),gen_no);
742 barf("evacuate: strange closure type %d", (int)(INFO_PTR_TO_STRUCT(info)->type));
748 /* -----------------------------------------------------------------------------
749 Evaluate a THUNK_SELECTOR if possible.
751 p points to a THUNK_SELECTOR that we want to evaluate. The
752 result of "evaluating" it will be evacuated and a pointer to the
753 to-space closure will be returned.
755 If the THUNK_SELECTOR could not be evaluated (its selectee is still
756 a THUNK, for example), then the THUNK_SELECTOR itself will be
758 -------------------------------------------------------------------------- */
760 unchain_thunk_selectors(StgSelector *p, StgClosure *val)
767 ASSERT(p->header.info == &stg_WHITEHOLE_info);
768 // val must be in to-space. Not always: when we recursively
769 // invoke eval_thunk_selector(), the recursive calls will not
770 // evacuate the value (because we want to select on the value,
771 // not evacuate it), so in this case val is in from-space.
772 // ASSERT(!HEAP_ALLOCED_GC(val) || Bdescr((P_)val)->gen_no > N || (Bdescr((P_)val)->flags & BF_EVACUATED));
774 prev = (StgSelector*)((StgClosure *)p)->payload[0];
776 // Update the THUNK_SELECTOR with an indirection to the
777 // value. The value is still in from-space at this stage.
779 // (old note: Why not do upd_evacuee(q,p)? Because we have an
780 // invariant that an EVACUATED closure always points to an
781 // object in the same or an older generation (required by
782 // the short-cut test in the EVACUATED case, below).
783 if ((StgClosure *)p == val) {
784 // must be a loop; just leave a BLACKHOLE in place. This
785 // can happen when we have a chain of selectors that
786 // eventually loops back on itself. We can't leave an
787 // indirection pointing to itself, and we want the program
788 // to deadlock if it ever enters this closure, so
789 // BLACKHOLE is correct.
791 // XXX we do not have BLACKHOLEs any more; replace with
792 // a THUNK_SELECTOR again. This will go into a loop if it is
793 // entered, and should result in a NonTermination exception.
794 ((StgThunk *)p)->payload[0] = val;
796 SET_INFO(p, &stg_sel_0_upd_info);
798 ((StgInd *)p)->indirectee = val;
800 SET_INFO(p, &stg_IND_info);
803 // For the purposes of LDV profiling, we have created an
805 LDV_RECORD_CREATE(p);
812 eval_thunk_selector (StgClosure **q, StgSelector * p, rtsBool evac)
813 // NB. for legacy reasons, p & q are swapped around :(
818 StgClosure *selectee;
819 StgSelector *prev_thunk_selector;
823 prev_thunk_selector = NULL;
824 // this is a chain of THUNK_SELECTORs that we are going to update
825 // to point to the value of the current THUNK_SELECTOR. Each
826 // closure on the chain is a WHITEHOLE, and points to the next in the
827 // chain with payload[0].
831 bd = Bdescr((StgPtr)p);
832 if (HEAP_ALLOCED_GC(p)) {
833 // If the THUNK_SELECTOR is in to-space or in a generation that we
834 // are not collecting, then bale out early. We won't be able to
835 // save any space in any case, and updating with an indirection is
836 // trickier in a non-collected gen: we would have to update the
838 if (bd->flags & BF_EVACUATED) {
839 unchain_thunk_selectors(prev_thunk_selector, (StgClosure *)p);
840 *q = (StgClosure *)p;
841 // shortcut, behave as for: if (evac) evacuate(q);
842 if (evac && bd->gen_no < gct->evac_gen_no) {
843 gct->failed_to_evac = rtsTrue;
844 TICK_GC_FAILED_PROMOTION();
848 // we don't update THUNK_SELECTORS in the compacted
849 // generation, because compaction does not remove the INDs
850 // that result, this causes confusion later
851 // (scavenge_mark_stack doesn't deal with IND). BEWARE! This
852 // bit is very tricky to get right. If you make changes
853 // around here, test by compiling stage 3 with +RTS -c -RTS.
854 if (bd->flags & BF_MARKED) {
855 // must call evacuate() to mark this closure if evac==rtsTrue
856 *q = (StgClosure *)p;
857 if (evac) evacuate(q);
858 unchain_thunk_selectors(prev_thunk_selector, (StgClosure *)p);
864 // WHITEHOLE the selector thunk, since it is now under evaluation.
865 // This is important to stop us going into an infinite loop if
866 // this selector thunk eventually refers to itself.
867 #if defined(THREADED_RTS)
868 // In threaded mode, we'll use WHITEHOLE to lock the selector
869 // thunk while we evaluate it.
872 info_ptr = xchg((StgPtr)&p->header.info, (W_)&stg_WHITEHOLE_info);
873 } while (info_ptr == (W_)&stg_WHITEHOLE_info);
875 // make sure someone else didn't get here first...
876 if (IS_FORWARDING_PTR(info_ptr) ||
877 INFO_PTR_TO_STRUCT(info_ptr)->type != THUNK_SELECTOR) {
878 // v. tricky now. The THUNK_SELECTOR has been evacuated
879 // by another thread, and is now either a forwarding ptr or IND.
880 // We need to extract ourselves from the current situation
881 // as cleanly as possible.
882 // - unlock the closure
883 // - update *q, we may have done *some* evaluation
884 // - if evac, we need to call evacuate(), because we
885 // need the write-barrier stuff.
886 // - undo the chain we've built to point to p.
887 SET_INFO(p, (const StgInfoTable *)info_ptr);
888 *q = (StgClosure *)p;
889 if (evac) evacuate(q);
890 unchain_thunk_selectors(prev_thunk_selector, (StgClosure *)p);
895 // Save the real info pointer (NOTE: not the same as get_itbl()).
896 info_ptr = (StgWord)p->header.info;
897 SET_INFO(p,&stg_WHITEHOLE_info);
900 field = INFO_PTR_TO_STRUCT(info_ptr)->layout.selector_offset;
902 // The selectee might be a constructor closure,
903 // so we untag the pointer.
904 selectee = UNTAG_CLOSURE(p->selectee);
907 // selectee now points to the closure that we're trying to select
908 // a field from. It may or may not be in to-space: we try not to
909 // end up in to-space, but it's impractical to avoid it in
910 // general. The compacting GC scatters to-space pointers in
911 // from-space during marking, for example. We rely on the property
912 // that evacuate() doesn't mind if it gets passed a to-space pointer.
914 info = (StgInfoTable*)selectee->header.info;
916 if (IS_FORWARDING_PTR(info)) {
917 // We don't follow pointers into to-space; the constructor
918 // has already been evacuated, so we won't save any space
919 // leaks by evaluating this selector thunk anyhow.
923 info = INFO_PTR_TO_STRUCT(info);
924 switch (info->type) {
926 goto bale_out; // about to be evacuated by another thread (or a loop).
935 case CONSTR_NOCAF_STATIC:
937 // check that the size is in range
938 ASSERT(field < (StgWord32)(info->layout.payload.ptrs +
939 info->layout.payload.nptrs));
941 // Select the right field from the constructor
942 val = selectee->payload[field];
945 // For the purposes of LDV profiling, we have destroyed
946 // the original selector thunk, p.
947 SET_INFO(p, (StgInfoTable *)info_ptr);
948 OVERWRITING_CLOSURE((StgClosure*)p);
949 SET_INFO(p, &stg_WHITEHOLE_info);
952 // the closure in val is now the "value" of the
953 // THUNK_SELECTOR in p. However, val may itself be a
954 // THUNK_SELECTOR, in which case we want to continue
955 // evaluating until we find the real value, and then
956 // update the whole chain to point to the value.
958 info_ptr = (StgWord)UNTAG_CLOSURE(val)->header.info;
959 if (!IS_FORWARDING_PTR(info_ptr))
961 info = INFO_PTR_TO_STRUCT(info_ptr);
962 switch (info->type) {
966 val = ((StgInd *)val)->indirectee;
969 ((StgClosure*)p)->payload[0] = (StgClosure *)prev_thunk_selector;
970 prev_thunk_selector = p;
971 p = (StgSelector*)val;
977 ((StgClosure*)p)->payload[0] = (StgClosure *)prev_thunk_selector;
978 prev_thunk_selector = p;
982 // update the other selectors in the chain *before*
983 // evacuating the value. This is necessary in the case
984 // where the value turns out to be one of the selectors
985 // in the chain (i.e. we have a loop), and evacuating it
986 // would corrupt the chain.
987 unchain_thunk_selectors(prev_thunk_selector, val);
989 // evacuate() cannot recurse through
990 // eval_thunk_selector(), because we know val is not
992 if (evac) evacuate(q);
999 // Again, we might need to untag a constructor.
1000 selectee = UNTAG_CLOSURE( ((StgInd *)selectee)->indirectee );
1006 const StgInfoTable *i;
1007 r = ((StgInd*)selectee)->indirectee;
1009 // establish whether this BH has been updated, and is now an
1010 // indirection, as in evacuate().
1011 if (GET_CLOSURE_TAG(r) == 0) {
1013 if (IS_FORWARDING_PTR(i)) {
1014 r = (StgClosure *)UN_FORWARDING_PTR(i);
1017 if (i == &stg_TSO_info
1018 || i == &stg_WHITEHOLE_info
1019 || i == &stg_BLOCKING_QUEUE_CLEAN_info
1020 || i == &stg_BLOCKING_QUEUE_DIRTY_info) {
1023 ASSERT(i != &stg_IND_info);
1026 selectee = UNTAG_CLOSURE( ((StgInd *)selectee)->indirectee );
1030 case THUNK_SELECTOR:
1034 // recursively evaluate this selector. We don't want to
1035 // recurse indefinitely, so we impose a depth bound.
1036 if (gct->thunk_selector_depth >= MAX_THUNK_SELECTOR_DEPTH) {
1040 gct->thunk_selector_depth++;
1041 // rtsFalse says "don't evacuate the result". It will,
1042 // however, update any THUNK_SELECTORs that are evaluated
1044 eval_thunk_selector(&val, (StgSelector*)selectee, rtsFalse);
1045 gct->thunk_selector_depth--;
1047 // did we actually manage to evaluate it?
1048 if (val == selectee) goto bale_out;
1050 // Of course this pointer might be tagged...
1051 selectee = UNTAG_CLOSURE(val);
1064 // not evaluated yet
1068 barf("eval_thunk_selector: strange selectee %d",
1073 // We didn't manage to evaluate this thunk; restore the old info
1074 // pointer. But don't forget: we still need to evacuate the thunk itself.
1075 SET_INFO(p, (const StgInfoTable *)info_ptr);
1076 // THREADED_RTS: we just unlocked the thunk, so another thread
1077 // might get in and update it. copy() will lock it again and
1078 // check whether it was updated in the meantime.
1079 *q = (StgClosure *)p;
1081 copy(q,(const StgInfoTable *)info_ptr,(StgClosure *)p,THUNK_SELECTOR_sizeW(),bd->dest_no);
1083 unchain_thunk_selectors(prev_thunk_selector, *q);