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"
24 #include "MarkStack.h"
27 #include "LdvProfile.h"
29 #if defined(PROF_SPIN) && defined(THREADED_RTS) && defined(PARALLEL_GC)
30 StgWord64 whitehole_spin = 0;
33 #if defined(THREADED_RTS) && !defined(PARALLEL_GC)
34 #define evacuate(p) evacuate1(p)
35 #define HEAP_ALLOCED_GC(p) HEAP_ALLOCED(p)
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)
43 /* Used to avoid long recursion due to selector thunks
45 #define MAX_THUNK_SELECTOR_DEPTH 16
47 static void eval_thunk_selector (StgClosure **q, StgSelector * p, rtsBool);
48 STATIC_INLINE void evacuate_large(StgPtr p);
50 /* -----------------------------------------------------------------------------
51 Allocate some space in which to copy an object.
52 -------------------------------------------------------------------------- */
55 alloc_for_copy (nat size, nat gen_no)
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
65 if (gen_no < gct->evac_gen_no) {
66 if (gct->eager_promotion) {
67 gen_no = gct->evac_gen_no;
69 gct->failed_to_evac = rtsTrue;
73 ws = &gct->gens[gen_no]; // zero memory references here
75 /* chain a new block onto the to-space for the destination gen if
79 ws->todo_free += size;
80 if (ws->todo_free > ws->todo_lim) {
81 to = todo_block_full(size, ws);
83 ASSERT(ws->todo_free >= ws->todo_bd->free && ws->todo_free <= ws->todo_lim);
88 /* -----------------------------------------------------------------------------
90 -------------------------------------------------------------------------- */
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)
99 to = alloc_for_copy(size,gen_no);
103 for (i = 1; i < size; i++) { // unroll for small i
107 // if (to+size+2 < bd->start + BLOCK_SIZE_W) {
108 // __builtin_prefetch(to + size + 2, 1);
111 #if defined(PARALLEL_GC)
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
118 *p = TAG_CLOSURE(tag,(StgClosure*)to);
122 src->header.info = (const StgInfoTable *)MK_FORWARDING_PTR(to);
123 *p = TAG_CLOSURE(tag,(StgClosure*)to);
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);
133 #if defined(PARALLEL_GC)
135 copy_tag_nolock(StgClosure **p, const StgInfoTable *info,
136 StgClosure *src, nat size, nat gen_no, StgWord tag)
141 to = alloc_for_copy(size,gen_no);
145 for (i = 1; i < size; i++) { // unroll for small i
149 // if somebody else reads the forwarding pointer, we better make
150 // sure there's a closure at the end of it.
152 *p = TAG_CLOSURE(tag,(StgClosure*)to);
153 src->header.info = (const StgInfoTable *)MK_FORWARDING_PTR(to);
155 // if (to+size+2 < bd->start + BLOCK_SIZE_W) {
156 // __builtin_prefetch(to + size + 2, 1);
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);
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.
172 copyPart(StgClosure **p, StgClosure *src, nat size_to_reserve,
173 nat size_to_copy, nat gen_no)
179 #if defined(PARALLEL_GC)
181 info = xchg((StgPtr)&src->header.info, (W_)&stg_WHITEHOLE_info);
182 if (info == (W_)&stg_WHITEHOLE_info) {
188 if (IS_FORWARDING_PTR(info)) {
189 src->header.info = (const StgInfoTable *)info;
190 evacuate(p); // does the failed_to_evac stuff
194 info = (W_)src->header.info;
197 to = alloc_for_copy(size_to_reserve, gen_no);
201 for (i = 1; i < size_to_copy; i++) { // unroll for small i
206 src->header.info = (const StgInfoTable*)MK_FORWARDING_PTR(to);
207 *p = (StgClosure *)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, nat gen_no)
227 copy_tag(p,info,src,size,gen_no,0);
230 /* -----------------------------------------------------------------------------
231 Evacuate a large object
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.
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)
245 generation *gen, *new_gen;
246 nat gen_no, new_gen_no;
252 ACQUIRE_SPIN_LOCK(&gen->sync);
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()).
259 if (gen_no < gct->evac_gen_no) {
260 gct->failed_to_evac = rtsTrue;
261 TICK_GC_FAILED_PROMOTION();
263 RELEASE_SPIN_LOCK(&gen->sync);
267 // remove from large_object list
269 bd->u.back->link = bd->link;
270 } else { // first object in the list
271 gen->large_objects = bd->link;
274 bd->link->u.back = bd->u.back;
277 /* link it on to the evacuated large object list of the destination gen
279 new_gen_no = bd->dest_no;
281 if (new_gen_no < gct->evac_gen_no) {
282 if (gct->eager_promotion) {
283 new_gen_no = gct->evac_gen_no;
285 gct->failed_to_evac = rtsTrue;
289 ws = &gct->gens[new_gen_no];
290 new_gen = &generations[new_gen_no];
292 bd->flags |= BF_EVACUATED;
293 initBdescr(bd, new_gen, new_gen->to);
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); }
306 bd->link = ws->todo_large_objects;
307 ws->todo_large_objects = bd;
310 RELEASE_SPIN_LOCK(&gen->sync);
313 /* ----------------------------------------------------------------------------
316 This is called (eventually) for every live object in the system.
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
323 if M >= gct->evac_gen
327 if M < gct->evac_gen evac to gct->evac_gen, step 0
329 if the object is already evacuated, then we check which generation
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.
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).
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 ------------------------------------------------------------------------- */
355 REGPARM1 GNUC_ATTR_HOT void
356 evacuate(StgClosure **p)
361 const StgInfoTable *info;
367 /* The tag and the pointer are split, to be merged after evacing */
368 tag = GET_CLOSURE_TAG(q);
369 q = UNTAG_CLOSURE(q);
371 ASSERTM(LOOKS_LIKE_CLOSURE_PTR(q), "invalid closure, info=%p", q->header.info);
373 if (!HEAP_ALLOCED_GC(q)) {
375 if (!major_gc) return;
378 switch (info->type) {
381 if (info->srt_bitmap != 0) {
382 if (*THUNK_STATIC_LINK((StgClosure *)q) == NULL) {
384 *THUNK_STATIC_LINK((StgClosure *)q) = gct->static_objects;
385 gct->static_objects = (StgClosure *)q;
388 link = (StgPtr)cas((StgPtr)THUNK_STATIC_LINK((StgClosure *)q),
390 (StgWord)gct->static_objects);
392 gct->static_objects = (StgClosure *)q;
400 if (info->srt_bitmap != 0 &&
401 *FUN_STATIC_LINK((StgClosure *)q) == NULL) {
403 *FUN_STATIC_LINK((StgClosure *)q) = gct->static_objects;
404 gct->static_objects = (StgClosure *)q;
407 link = (StgPtr)cas((StgPtr)FUN_STATIC_LINK((StgClosure *)q),
409 (StgWord)gct->static_objects);
411 gct->static_objects = (StgClosure *)q;
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).
422 if (*IND_STATIC_LINK((StgClosure *)q) == NULL) {
424 *IND_STATIC_LINK((StgClosure *)q) = gct->static_objects;
425 gct->static_objects = (StgClosure *)q;
428 link = (StgPtr)cas((StgPtr)IND_STATIC_LINK((StgClosure *)q),
430 (StgWord)gct->static_objects);
432 gct->static_objects = (StgClosure *)q;
439 if (*STATIC_LINK(info,(StgClosure *)q) == NULL) {
441 *STATIC_LINK(info,(StgClosure *)q) = gct->static_objects;
442 gct->static_objects = (StgClosure *)q;
445 link = (StgPtr)cas((StgPtr)STATIC_LINK(info,(StgClosure *)q),
447 (StgWord)gct->static_objects);
449 gct->static_objects = (StgClosure *)q;
453 /* I am assuming that static_objects pointers are not
454 * written to other objects, and thus, no need to retag. */
457 case CONSTR_NOCAF_STATIC:
458 /* no need to put these on the static linked list, they don't need
464 barf("evacuate(static): strange closure type %d", (int)(info->type));
470 if ((bd->flags & (BF_LARGE | BF_MARKED | BF_EVACUATED)) != 0) {
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();
489 /* evacuate large objects by re-linking them onto a different list.
491 if (bd->flags & BF_LARGE) {
492 evacuate_large((P_)q);
496 /* If the object is in a gen that we're compacting, then we
497 * need to use an alternative evacuate procedure.
499 if (!is_marked((P_)q,bd)) {
501 push_mark_stack((P_)q);
506 gen_no = bd->dest_no;
508 info = q->header.info;
509 if (IS_FORWARDING_PTR(info))
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.
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.
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();
538 switch (INFO_PTR_TO_STRUCT(info)->type) {
547 copy(p,info,q,sizeW_fromITBL(INFO_PTR_TO_STRUCT(info)),gen_no);
550 // For ints and chars of low value, save space by replacing references to
551 // these with closures with references to common, shared ones in the RTS.
553 // * Except when compiling into Windows DLLs which don't support cross-package
554 // data references very well.
558 #if defined(__PIC__) && defined(mingw32_HOST_OS)
559 copy_tag_nolock(p,info,q,sizeofW(StgHeader)+1,gen_no,tag);
561 StgWord w = (StgWord)q->payload[0];
562 if (info == Czh_con_info &&
563 // unsigned, so always true: (StgChar)w >= MIN_CHARLIKE &&
564 (StgChar)w <= MAX_CHARLIKE) {
565 *p = TAG_CLOSURE(tag,
566 (StgClosure *)CHARLIKE_CLOSURE((StgChar)w)
569 else if (info == Izh_con_info &&
570 (StgInt)w >= MIN_INTLIKE && (StgInt)w <= MAX_INTLIKE) {
571 *p = TAG_CLOSURE(tag,
572 (StgClosure *)INTLIKE_CLOSURE((StgInt)w)
576 copy_tag_nolock(p,info,q,sizeofW(StgHeader)+1,gen_no,tag);
585 copy_tag_nolock(p,info,q,sizeofW(StgHeader)+1,gen_no,tag);
590 copy(p,info,q,sizeofW(StgThunk)+1,gen_no);
596 #ifdef NO_PROMOTE_THUNKS
599 copy(p,info,q,sizeofW(StgThunk)+2,gen_no);
607 copy_tag_nolock(p,info,q,sizeofW(StgHeader)+2,gen_no,tag);
611 copy_tag_nolock(p,info,q,sizeofW(StgHeader)+2,gen_no,tag);
615 copy(p,info,q,thunk_sizeW_fromITBL(INFO_PTR_TO_STRUCT(info)),gen_no);
621 copy_tag_nolock(p,info,q,sizeW_fromITBL(INFO_PTR_TO_STRUCT(info)),gen_no,tag);
627 const StgInfoTable *i;
628 r = ((StgInd*)q)->indirectee;
629 if (GET_CLOSURE_TAG(r) == 0) {
631 if (IS_FORWARDING_PTR(i)) {
632 r = (StgClosure *)UN_FORWARDING_PTR(i);
635 if (i == &stg_TSO_info
636 || i == &stg_WHITEHOLE_info
637 || i == &stg_BLOCKING_QUEUE_CLEAN_info
638 || i == &stg_BLOCKING_QUEUE_DIRTY_info) {
639 copy(p,info,q,sizeofW(StgInd),gen_no);
642 ASSERT(i != &stg_IND_info);
653 copy(p,info,q,sizeW_fromITBL(INFO_PTR_TO_STRUCT(info)),gen_no);
657 copy(p,info,q,bco_sizeW((StgBCO *)q),gen_no);
661 eval_thunk_selector(p, (StgSelector *)q, rtsTrue);
665 // follow chains of indirections, don't evacuate them
666 q = ((StgInd*)q)->indirectee;
675 case UNDERFLOW_FRAME:
678 case CATCH_STM_FRAME:
679 case CATCH_RETRY_FRAME:
680 case ATOMICALLY_FRAME:
681 // shouldn't see these
682 barf("evacuate: stack frame at %p\n", q);
685 copy(p,info,q,pap_sizeW((StgPAP*)q),gen_no);
689 copy(p,info,q,ap_sizeW((StgAP*)q),gen_no);
693 copy(p,info,q,ap_stack_sizeW((StgAP_STACK*)q),gen_no);
697 // just copy the block
698 copy(p,info,q,arr_words_sizeW((StgArrWords *)q),gen_no);
701 case MUT_ARR_PTRS_CLEAN:
702 case MUT_ARR_PTRS_DIRTY:
703 case MUT_ARR_PTRS_FROZEN:
704 case MUT_ARR_PTRS_FROZEN0:
705 // just copy the block
706 copy(p,info,q,mut_arr_ptrs_sizeW((StgMutArrPtrs *)q),gen_no);
710 copy(p,info,q,sizeofW(StgTSO),gen_no);
715 StgStack *stack = (StgStack *)q;
717 /* To evacuate a small STACK, we need to adjust the stack pointer
724 mine = copyPart(p,(StgClosure *)stack, stack_sizeW(stack),
725 sizeofW(StgStack), gen_no);
727 new_stack = (StgStack *)*p;
728 move_STACK(stack, new_stack);
729 for (r = stack->sp, s = new_stack->sp;
730 r < stack->stack + stack->stack_size;) {
739 copy(p,info,q,sizeofW(StgTRecChunk),gen_no);
743 barf("evacuate: strange closure type %d", (int)(INFO_PTR_TO_STRUCT(info)->type));
749 /* -----------------------------------------------------------------------------
750 Evaluate a THUNK_SELECTOR if possible.
752 p points to a THUNK_SELECTOR that we want to evaluate. The
753 result of "evaluating" it will be evacuated and a pointer to the
754 to-space closure will be returned.
756 If the THUNK_SELECTOR could not be evaluated (its selectee is still
757 a THUNK, for example), then the THUNK_SELECTOR itself will be
759 -------------------------------------------------------------------------- */
761 unchain_thunk_selectors(StgSelector *p, StgClosure *val)
768 ASSERT(p->header.info == &stg_WHITEHOLE_info);
769 // val must be in to-space. Not always: when we recursively
770 // invoke eval_thunk_selector(), the recursive calls will not
771 // evacuate the value (because we want to select on the value,
772 // not evacuate it), so in this case val is in from-space.
773 // ASSERT(!HEAP_ALLOCED_GC(val) || Bdescr((P_)val)->gen_no > N || (Bdescr((P_)val)->flags & BF_EVACUATED));
775 prev = (StgSelector*)((StgClosure *)p)->payload[0];
777 // Update the THUNK_SELECTOR with an indirection to the
778 // value. The value is still in from-space at this stage.
780 // (old note: Why not do upd_evacuee(q,p)? Because we have an
781 // invariant that an EVACUATED closure always points to an
782 // object in the same or an older generation (required by
783 // the short-cut test in the EVACUATED case, below).
784 if ((StgClosure *)p == val) {
785 // must be a loop; just leave a BLACKHOLE in place. This
786 // can happen when we have a chain of selectors that
787 // eventually loops back on itself. We can't leave an
788 // indirection pointing to itself, and we want the program
789 // to deadlock if it ever enters this closure, so
790 // BLACKHOLE is correct.
792 // XXX we do not have BLACKHOLEs any more; replace with
793 // a THUNK_SELECTOR again. This will go into a loop if it is
794 // entered, and should result in a NonTermination exception.
795 ((StgThunk *)p)->payload[0] = val;
797 SET_INFO(p, &stg_sel_0_upd_info);
799 ((StgInd *)p)->indirectee = val;
801 SET_INFO(p, &stg_IND_info);
804 // For the purposes of LDV profiling, we have created an
806 LDV_RECORD_CREATE(p);
813 eval_thunk_selector (StgClosure **q, StgSelector * p, rtsBool evac)
814 // NB. for legacy reasons, p & q are swapped around :(
819 StgClosure *selectee;
820 StgSelector *prev_thunk_selector;
824 prev_thunk_selector = NULL;
825 // this is a chain of THUNK_SELECTORs that we are going to update
826 // to point to the value of the current THUNK_SELECTOR. Each
827 // closure on the chain is a WHITEHOLE, and points to the next in the
828 // chain with payload[0].
832 bd = Bdescr((StgPtr)p);
833 if (HEAP_ALLOCED_GC(p)) {
834 // If the THUNK_SELECTOR is in to-space or in a generation that we
835 // are not collecting, then bale out early. We won't be able to
836 // save any space in any case, and updating with an indirection is
837 // trickier in a non-collected gen: we would have to update the
839 if (bd->flags & BF_EVACUATED) {
840 unchain_thunk_selectors(prev_thunk_selector, (StgClosure *)p);
841 *q = (StgClosure *)p;
842 // shortcut, behave as for: if (evac) evacuate(q);
843 if (evac && bd->gen_no < gct->evac_gen_no) {
844 gct->failed_to_evac = rtsTrue;
845 TICK_GC_FAILED_PROMOTION();
849 // we don't update THUNK_SELECTORS in the compacted
850 // generation, because compaction does not remove the INDs
851 // that result, this causes confusion later
852 // (scavenge_mark_stack doesn't deal with IND). BEWARE! This
853 // bit is very tricky to get right. If you make changes
854 // around here, test by compiling stage 3 with +RTS -c -RTS.
855 if (bd->flags & BF_MARKED) {
856 // must call evacuate() to mark this closure if evac==rtsTrue
857 *q = (StgClosure *)p;
858 if (evac) evacuate(q);
859 unchain_thunk_selectors(prev_thunk_selector, (StgClosure *)p);
865 // WHITEHOLE the selector thunk, since it is now under evaluation.
866 // This is important to stop us going into an infinite loop if
867 // this selector thunk eventually refers to itself.
868 #if defined(THREADED_RTS)
869 // In threaded mode, we'll use WHITEHOLE to lock the selector
870 // thunk while we evaluate it.
873 info_ptr = xchg((StgPtr)&p->header.info, (W_)&stg_WHITEHOLE_info);
874 } while (info_ptr == (W_)&stg_WHITEHOLE_info);
876 // make sure someone else didn't get here first...
877 if (IS_FORWARDING_PTR(info_ptr) ||
878 INFO_PTR_TO_STRUCT(info_ptr)->type != THUNK_SELECTOR) {
879 // v. tricky now. The THUNK_SELECTOR has been evacuated
880 // by another thread, and is now either a forwarding ptr or IND.
881 // We need to extract ourselves from the current situation
882 // as cleanly as possible.
883 // - unlock the closure
884 // - update *q, we may have done *some* evaluation
885 // - if evac, we need to call evacuate(), because we
886 // need the write-barrier stuff.
887 // - undo the chain we've built to point to p.
888 SET_INFO(p, (const StgInfoTable *)info_ptr);
889 *q = (StgClosure *)p;
890 if (evac) evacuate(q);
891 unchain_thunk_selectors(prev_thunk_selector, (StgClosure *)p);
896 // Save the real info pointer (NOTE: not the same as get_itbl()).
897 info_ptr = (StgWord)p->header.info;
898 SET_INFO(p,&stg_WHITEHOLE_info);
901 field = INFO_PTR_TO_STRUCT(info_ptr)->layout.selector_offset;
903 // The selectee might be a constructor closure,
904 // so we untag the pointer.
905 selectee = UNTAG_CLOSURE(p->selectee);
908 // selectee now points to the closure that we're trying to select
909 // a field from. It may or may not be in to-space: we try not to
910 // end up in to-space, but it's impractical to avoid it in
911 // general. The compacting GC scatters to-space pointers in
912 // from-space during marking, for example. We rely on the property
913 // that evacuate() doesn't mind if it gets passed a to-space pointer.
915 info = (StgInfoTable*)selectee->header.info;
917 if (IS_FORWARDING_PTR(info)) {
918 // We don't follow pointers into to-space; the constructor
919 // has already been evacuated, so we won't save any space
920 // leaks by evaluating this selector thunk anyhow.
924 info = INFO_PTR_TO_STRUCT(info);
925 switch (info->type) {
927 goto bale_out; // about to be evacuated by another thread (or a loop).
936 case CONSTR_NOCAF_STATIC:
938 // check that the size is in range
939 ASSERT(field < (StgWord32)(info->layout.payload.ptrs +
940 info->layout.payload.nptrs));
942 // Select the right field from the constructor
943 val = selectee->payload[field];
946 // For the purposes of LDV profiling, we have destroyed
947 // the original selector thunk, p.
948 SET_INFO(p, (StgInfoTable *)info_ptr);
949 OVERWRITING_CLOSURE((StgClosure*)p);
950 SET_INFO(p, &stg_WHITEHOLE_info);
953 // the closure in val is now the "value" of the
954 // THUNK_SELECTOR in p. However, val may itself be a
955 // THUNK_SELECTOR, in which case we want to continue
956 // evaluating until we find the real value, and then
957 // update the whole chain to point to the value.
959 info_ptr = (StgWord)UNTAG_CLOSURE(val)->header.info;
960 if (!IS_FORWARDING_PTR(info_ptr))
962 info = INFO_PTR_TO_STRUCT(info_ptr);
963 switch (info->type) {
967 val = ((StgInd *)val)->indirectee;
970 ((StgClosure*)p)->payload[0] = (StgClosure *)prev_thunk_selector;
971 prev_thunk_selector = p;
972 p = (StgSelector*)val;
978 ((StgClosure*)p)->payload[0] = (StgClosure *)prev_thunk_selector;
979 prev_thunk_selector = p;
983 // update the other selectors in the chain *before*
984 // evacuating the value. This is necessary in the case
985 // where the value turns out to be one of the selectors
986 // in the chain (i.e. we have a loop), and evacuating it
987 // would corrupt the chain.
988 unchain_thunk_selectors(prev_thunk_selector, val);
990 // evacuate() cannot recurse through
991 // eval_thunk_selector(), because we know val is not
993 if (evac) evacuate(q);
1000 // Again, we might need to untag a constructor.
1001 selectee = UNTAG_CLOSURE( ((StgInd *)selectee)->indirectee );
1007 const StgInfoTable *i;
1008 r = ((StgInd*)selectee)->indirectee;
1010 // establish whether this BH has been updated, and is now an
1011 // indirection, as in evacuate().
1012 if (GET_CLOSURE_TAG(r) == 0) {
1014 if (IS_FORWARDING_PTR(i)) {
1015 r = (StgClosure *)UN_FORWARDING_PTR(i);
1018 if (i == &stg_TSO_info
1019 || i == &stg_WHITEHOLE_info
1020 || i == &stg_BLOCKING_QUEUE_CLEAN_info
1021 || i == &stg_BLOCKING_QUEUE_DIRTY_info) {
1024 ASSERT(i != &stg_IND_info);
1027 selectee = UNTAG_CLOSURE( ((StgInd *)selectee)->indirectee );
1031 case THUNK_SELECTOR:
1035 // recursively evaluate this selector. We don't want to
1036 // recurse indefinitely, so we impose a depth bound.
1037 if (gct->thunk_selector_depth >= MAX_THUNK_SELECTOR_DEPTH) {
1041 gct->thunk_selector_depth++;
1042 // rtsFalse says "don't evacuate the result". It will,
1043 // however, update any THUNK_SELECTORs that are evaluated
1045 eval_thunk_selector(&val, (StgSelector*)selectee, rtsFalse);
1046 gct->thunk_selector_depth--;
1048 // did we actually manage to evaluate it?
1049 if (val == selectee) goto bale_out;
1051 // Of course this pointer might be tagged...
1052 selectee = UNTAG_CLOSURE(val);
1065 // not evaluated yet
1069 barf("eval_thunk_selector: strange selectee %d",
1074 // We didn't manage to evaluate this thunk; restore the old info
1075 // pointer. But don't forget: we still need to evacuate the thunk itself.
1076 SET_INFO(p, (const StgInfoTable *)info_ptr);
1077 // THREADED_RTS: we just unlocked the thunk, so another thread
1078 // might get in and update it. copy() will lock it again and
1079 // check whether it was updated in the meantime.
1080 *q = (StgClosure *)p;
1082 copy(q,(const StgInfoTable *)info_ptr,(StgClosure *)p,THUNK_SELECTOR_sizeW(),bd->dest_no);
1084 unchain_thunk_selectors(prev_thunk_selector, *q);