Remove vectored returns.
[ghc-hetmet.git] / rts / sm / Evac.c
1 /* -----------------------------------------------------------------------------
2  *
3  * (c) The GHC Team 1998-2006
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 "Rts.h"
15 #include "Storage.h"
16 #include "MBlock.h"
17 #include "Evac.h"
18 #include "GC.h"
19 #include "GCUtils.h"
20 #include "Compact.h"
21 #include "Prelude.h"
22 #include "LdvProfile.h"
23
24 /* Used to avoid long recursion due to selector thunks
25  */
26 lnat thunk_selector_depth = 0;
27 #define MAX_THUNK_SELECTOR_DEPTH 8
28
29 static StgClosure * eval_thunk_selector ( nat field, StgSelector * p );
30
31 STATIC_INLINE void 
32 upd_evacuee(StgClosure *p, StgClosure *dest)
33 {
34     // not true: (ToDo: perhaps it should be)
35     // ASSERT(Bdescr((P_)dest)->flags & BF_EVACUATED);
36     SET_INFO(p, &stg_EVACUATED_info);
37     ((StgEvacuated *)p)->evacuee = dest;
38 }
39
40
41 STATIC_INLINE StgClosure *
42 copy(StgClosure *src, nat size, step *stp)
43 {
44   StgPtr to, from;
45   nat i;
46 #ifdef PROFILING
47   // @LDV profiling
48   nat size_org = size;
49 #endif
50
51   TICK_GC_WORDS_COPIED(size);
52   /* Find out where we're going, using the handy "to" pointer in 
53    * the step of the source object.  If it turns out we need to
54    * evacuate to an older generation, adjust it here (see comment
55    * by evacuate()).
56    */
57   if (stp->gen_no < evac_gen) {
58       if (eager_promotion) {
59           stp = &generations[evac_gen].steps[0];
60       } else {
61           failed_to_evac = rtsTrue;
62       }
63   }
64
65   /* chain a new block onto the to-space for the destination step if
66    * necessary.
67    */
68   if (stp->hp + size >= stp->hpLim) {
69     gc_alloc_block(stp);
70   }
71
72   to = stp->hp;
73   from = (StgPtr)src;
74   stp->hp = to + size;
75   for (i = 0; i < size; i++) { // unroll for small i
76       to[i] = from[i];
77   }
78   upd_evacuee((StgClosure *)from,(StgClosure *)to);
79
80 #ifdef PROFILING
81   // We store the size of the just evacuated object in the LDV word so that
82   // the profiler can guess the position of the next object later.
83   SET_EVACUAEE_FOR_LDV(from, size_org);
84 #endif
85   return (StgClosure *)to;
86 }
87
88 // Same as copy() above, except the object will be allocated in memory
89 // that will not be scavenged.  Used for object that have no pointer
90 // fields.
91 STATIC_INLINE StgClosure *
92 copy_noscav(StgClosure *src, nat size, step *stp)
93 {
94   StgPtr to, from;
95   nat i;
96 #ifdef PROFILING
97   // @LDV profiling
98   nat size_org = size;
99 #endif
100
101   TICK_GC_WORDS_COPIED(size);
102   /* Find out where we're going, using the handy "to" pointer in 
103    * the step of the source object.  If it turns out we need to
104    * evacuate to an older generation, adjust it here (see comment
105    * by evacuate()).
106    */
107   if (stp->gen_no < evac_gen) {
108       if (eager_promotion) {
109           stp = &generations[evac_gen].steps[0];
110       } else {
111           failed_to_evac = rtsTrue;
112       }
113   }
114
115   /* chain a new block onto the to-space for the destination step if
116    * necessary.
117    */
118   if (stp->scavd_hp + size >= stp->scavd_hpLim) {
119     gc_alloc_scavd_block(stp);
120   }
121
122   to = stp->scavd_hp;
123   from = (StgPtr)src;
124   stp->scavd_hp = to + size;
125   for (i = 0; i < size; i++) { // unroll for small i
126       to[i] = from[i];
127   }
128   upd_evacuee((StgClosure *)from,(StgClosure *)to);
129
130 #ifdef PROFILING
131   // We store the size of the just evacuated object in the LDV word so that
132   // the profiler can guess the position of the next object later.
133   SET_EVACUAEE_FOR_LDV(from, size_org);
134 #endif
135   return (StgClosure *)to;
136 }
137
138 /* Special version of copy() for when we only want to copy the info
139  * pointer of an object, but reserve some padding after it.  This is
140  * used to optimise evacuation of BLACKHOLEs.
141  */
142
143
144 static StgClosure *
145 copyPart(StgClosure *src, nat size_to_reserve, nat size_to_copy, step *stp)
146 {
147   P_ dest, to, from;
148 #ifdef PROFILING
149   // @LDV profiling
150   nat size_to_copy_org = size_to_copy;
151 #endif
152
153   TICK_GC_WORDS_COPIED(size_to_copy);
154   if (stp->gen_no < evac_gen) {
155       if (eager_promotion) {
156           stp = &generations[evac_gen].steps[0];
157       } else {
158           failed_to_evac = rtsTrue;
159       }
160   }
161
162   if (stp->hp + size_to_reserve >= stp->hpLim) {
163     gc_alloc_block(stp);
164   }
165
166   for(to = stp->hp, from = (P_)src; size_to_copy>0; --size_to_copy) {
167     *to++ = *from++;
168   }
169   
170   dest = stp->hp;
171   stp->hp += size_to_reserve;
172   upd_evacuee(src,(StgClosure *)dest);
173 #ifdef PROFILING
174   // We store the size of the just evacuated object in the LDV word so that
175   // the profiler can guess the position of the next object later.
176   // size_to_copy_org is wrong because the closure already occupies size_to_reserve
177   // words.
178   SET_EVACUAEE_FOR_LDV(src, size_to_reserve);
179   // fill the slop
180   if (size_to_reserve - size_to_copy_org > 0)
181     LDV_FILL_SLOP(stp->hp - 1, (int)(size_to_reserve - size_to_copy_org)); 
182 #endif
183   return (StgClosure *)dest;
184 }
185
186
187 /* -----------------------------------------------------------------------------
188    Evacuate a large object
189
190    This just consists of removing the object from the (doubly-linked)
191    step->large_objects list, and linking it on to the (singly-linked)
192    step->new_large_objects list, from where it will be scavenged later.
193
194    Convention: bd->flags has BF_EVACUATED set for a large object
195    that has been evacuated, or unset otherwise.
196    -------------------------------------------------------------------------- */
197
198
199 STATIC_INLINE void
200 evacuate_large(StgPtr p)
201 {
202   bdescr *bd = Bdescr(p);
203   step *stp;
204
205   // object must be at the beginning of the block (or be a ByteArray)
206   ASSERT(get_itbl((StgClosure *)p)->type == ARR_WORDS ||
207          (((W_)p & BLOCK_MASK) == 0));
208
209   // already evacuated? 
210   if (bd->flags & BF_EVACUATED) { 
211     /* Don't forget to set the failed_to_evac flag if we didn't get
212      * the desired destination (see comments in evacuate()).
213      */
214     if (bd->gen_no < evac_gen) {
215       failed_to_evac = rtsTrue;
216       TICK_GC_FAILED_PROMOTION();
217     }
218     return;
219   }
220
221   stp = bd->step;
222   // remove from large_object list 
223   if (bd->u.back) {
224     bd->u.back->link = bd->link;
225   } else { // first object in the list 
226     stp->large_objects = bd->link;
227   }
228   if (bd->link) {
229     bd->link->u.back = bd->u.back;
230   }
231   
232   /* link it on to the evacuated large object list of the destination step
233    */
234   stp = bd->step->to;
235   if (stp->gen_no < evac_gen) {
236       if (eager_promotion) {
237           stp = &generations[evac_gen].steps[0];
238       } else {
239           failed_to_evac = rtsTrue;
240       }
241   }
242
243   bd->step = stp;
244   bd->gen_no = stp->gen_no;
245   bd->link = stp->new_large_objects;
246   stp->new_large_objects = bd;
247   bd->flags |= BF_EVACUATED;
248 }
249
250 /* -----------------------------------------------------------------------------
251    Evacuate
252
253    This is called (eventually) for every live object in the system.
254
255    The caller to evacuate specifies a desired generation in the
256    evac_gen global variable.  The following conditions apply to
257    evacuating an object which resides in generation M when we're
258    collecting up to generation N
259
260    if  M >= evac_gen 
261            if  M > N     do nothing
262            else          evac to step->to
263
264    if  M < evac_gen      evac to evac_gen, step 0
265
266    if the object is already evacuated, then we check which generation
267    it now resides in.
268
269    if  M >= evac_gen     do nothing
270    if  M <  evac_gen     set failed_to_evac flag to indicate that we
271                          didn't manage to evacuate this object into evac_gen.
272
273
274    OPTIMISATION NOTES:
275
276    evacuate() is the single most important function performance-wise
277    in the GC.  Various things have been tried to speed it up, but as
278    far as I can tell the code generated by gcc 3.2 with -O2 is about
279    as good as it's going to get.  We pass the argument to evacuate()
280    in a register using the 'regparm' attribute (see the prototype for
281    evacuate() near the top of this file).
282
283    Changing evacuate() to take an (StgClosure **) rather than
284    returning the new pointer seems attractive, because we can avoid
285    writing back the pointer when it hasn't changed (eg. for a static
286    object, or an object in a generation > N).  However, I tried it and
287    it doesn't help.  One reason is that the (StgClosure **) pointer
288    gets spilled to the stack inside evacuate(), resulting in far more
289    extra reads/writes than we save.
290    -------------------------------------------------------------------------- */
291
292 REGPARM1 StgClosure *
293 evacuate(StgClosure *q)
294 {
295   bdescr *bd = NULL;
296   step *stp;
297   const StgInfoTable *info;
298
299 loop:
300   ASSERT(LOOKS_LIKE_CLOSURE_PTR(q));
301
302   if (!HEAP_ALLOCED(q)) {
303
304       if (!major_gc) return q;
305
306       info = get_itbl(q);
307       switch (info->type) {
308
309       case THUNK_STATIC:
310           if (info->srt_bitmap != 0 && 
311               *THUNK_STATIC_LINK((StgClosure *)q) == NULL) {
312               *THUNK_STATIC_LINK((StgClosure *)q) = static_objects;
313               static_objects = (StgClosure *)q;
314           }
315           return q;
316           
317       case FUN_STATIC:
318           if (info->srt_bitmap != 0 && 
319               *FUN_STATIC_LINK((StgClosure *)q) == NULL) {
320               *FUN_STATIC_LINK((StgClosure *)q) = static_objects;
321               static_objects = (StgClosure *)q;
322           }
323           return q;
324           
325       case IND_STATIC:
326           /* If q->saved_info != NULL, then it's a revertible CAF - it'll be
327            * on the CAF list, so don't do anything with it here (we'll
328            * scavenge it later).
329            */
330           if (((StgIndStatic *)q)->saved_info == NULL
331               && *IND_STATIC_LINK((StgClosure *)q) == NULL) {
332               *IND_STATIC_LINK((StgClosure *)q) = static_objects;
333               static_objects = (StgClosure *)q;
334           }
335           return q;
336           
337       case CONSTR_STATIC:
338           if (*STATIC_LINK(info,(StgClosure *)q) == NULL) {
339               *STATIC_LINK(info,(StgClosure *)q) = static_objects;
340               static_objects = (StgClosure *)q;
341           }
342           return q;
343           
344       case CONSTR_NOCAF_STATIC:
345           /* no need to put these on the static linked list, they don't need
346            * to be scavenged.
347            */
348           return q;
349           
350       default:
351           barf("evacuate(static): strange closure type %d", (int)(info->type));
352       }
353   }
354
355   bd = Bdescr((P_)q);
356
357   if (bd->gen_no > N) {
358       /* Can't evacuate this object, because it's in a generation
359        * older than the ones we're collecting.  Let's hope that it's
360        * in evac_gen or older, or we will have to arrange to track
361        * this pointer using the mutable list.
362        */
363       if (bd->gen_no < evac_gen) {
364           // nope 
365           failed_to_evac = rtsTrue;
366           TICK_GC_FAILED_PROMOTION();
367       }
368       return q;
369   }
370
371   if ((bd->flags & (BF_LARGE | BF_COMPACTED | BF_EVACUATED)) != 0) {
372
373       /* pointer into to-space: just return it.  This normally
374        * shouldn't happen, but alllowing it makes certain things
375        * slightly easier (eg. the mutable list can contain the same
376        * object twice, for example).
377        */
378       if (bd->flags & BF_EVACUATED) {
379           if (bd->gen_no < evac_gen) {
380               failed_to_evac = rtsTrue;
381               TICK_GC_FAILED_PROMOTION();
382           }
383           return q;
384       }
385
386       /* evacuate large objects by re-linking them onto a different list.
387        */
388       if (bd->flags & BF_LARGE) {
389           info = get_itbl(q);
390           if (info->type == TSO && 
391               ((StgTSO *)q)->what_next == ThreadRelocated) {
392               q = (StgClosure *)((StgTSO *)q)->link;
393               goto loop;
394           }
395           evacuate_large((P_)q);
396           return q;
397       }
398       
399       /* If the object is in a step that we're compacting, then we
400        * need to use an alternative evacuate procedure.
401        */
402       if (bd->flags & BF_COMPACTED) {
403           if (!is_marked((P_)q,bd)) {
404               mark((P_)q,bd);
405               if (mark_stack_full()) {
406                   mark_stack_overflowed = rtsTrue;
407                   reset_mark_stack();
408               }
409               push_mark_stack((P_)q);
410           }
411           return q;
412       }
413   }
414       
415   stp = bd->step->to;
416
417   info = get_itbl(q);
418   
419   switch (info->type) {
420
421   case MUT_VAR_CLEAN:
422   case MUT_VAR_DIRTY:
423   case MVAR:
424       return copy(q,sizeW_fromITBL(info),stp);
425
426   case CONSTR_0_1:
427   { 
428       StgWord w = (StgWord)q->payload[0];
429       if (q->header.info == Czh_con_info &&
430           // unsigned, so always true:  (StgChar)w >= MIN_CHARLIKE &&  
431           (StgChar)w <= MAX_CHARLIKE) {
432           return (StgClosure *)CHARLIKE_CLOSURE((StgChar)w);
433       }
434       if (q->header.info == Izh_con_info &&
435           (StgInt)w >= MIN_INTLIKE && (StgInt)w <= MAX_INTLIKE) {
436           return (StgClosure *)INTLIKE_CLOSURE((StgInt)w);
437       }
438       // else
439       return copy_noscav(q,sizeofW(StgHeader)+1,stp);
440   }
441
442   case FUN_0_1:
443   case FUN_1_0:
444   case CONSTR_1_0:
445     return copy(q,sizeofW(StgHeader)+1,stp);
446
447   case THUNK_1_0:
448   case THUNK_0_1:
449     return copy(q,sizeofW(StgThunk)+1,stp);
450
451   case THUNK_1_1:
452   case THUNK_2_0:
453   case THUNK_0_2:
454 #ifdef NO_PROMOTE_THUNKS
455     if (bd->gen_no == 0 && 
456         bd->step->no != 0 &&
457         bd->step->no == generations[bd->gen_no].n_steps-1) {
458       stp = bd->step;
459     }
460 #endif
461     return copy(q,sizeofW(StgThunk)+2,stp);
462
463   case FUN_1_1:
464   case FUN_2_0:
465   case CONSTR_1_1:
466   case CONSTR_2_0:
467   case FUN_0_2:
468     return copy(q,sizeofW(StgHeader)+2,stp);
469
470   case CONSTR_0_2:
471     return copy_noscav(q,sizeofW(StgHeader)+2,stp);
472
473   case THUNK:
474     return copy(q,thunk_sizeW_fromITBL(info),stp);
475
476   case FUN:
477   case CONSTR:
478   case IND_PERM:
479   case IND_OLDGEN_PERM:
480   case WEAK:
481   case STABLE_NAME:
482     return copy(q,sizeW_fromITBL(info),stp);
483
484   case BCO:
485       return copy(q,bco_sizeW((StgBCO *)q),stp);
486
487   case CAF_BLACKHOLE:
488   case SE_CAF_BLACKHOLE:
489   case SE_BLACKHOLE:
490   case BLACKHOLE:
491     return copyPart(q,BLACKHOLE_sizeW(),sizeofW(StgHeader),stp);
492
493   case THUNK_SELECTOR:
494     {
495         StgClosure *p;
496         const StgInfoTable *info_ptr;
497
498         if (thunk_selector_depth > MAX_THUNK_SELECTOR_DEPTH) {
499             return copy(q,THUNK_SELECTOR_sizeW(),stp);
500         }
501
502         // stashed away for LDV profiling, see below
503         info_ptr = q->header.info;
504
505         p = eval_thunk_selector(info->layout.selector_offset,
506                                 (StgSelector *)q);
507
508         if (p == NULL) {
509             return copy(q,THUNK_SELECTOR_sizeW(),stp);
510         } else {
511             StgClosure *val;
512             // q is still BLACKHOLE'd.
513             thunk_selector_depth++;
514             val = evacuate(p);
515             thunk_selector_depth--;
516
517 #ifdef PROFILING
518             // For the purposes of LDV profiling, we have destroyed
519             // the original selector thunk.
520             SET_INFO(q, info_ptr);
521             LDV_RECORD_DEAD_FILL_SLOP_DYNAMIC(q);
522 #endif
523
524             // Update the THUNK_SELECTOR with an indirection to the
525             // EVACUATED closure now at p.  Why do this rather than
526             // upd_evacuee(q,p)?  Because we have an invariant that an
527             // EVACUATED closure always points to an object in the
528             // same or an older generation (required by the short-cut
529             // test in the EVACUATED case, below).
530             SET_INFO(q, &stg_IND_info);
531             ((StgInd *)q)->indirectee = p;
532
533             // For the purposes of LDV profiling, we have created an
534             // indirection.
535             LDV_RECORD_CREATE(q);
536
537             return val;
538         }
539     }
540
541   case IND:
542   case IND_OLDGEN:
543     // follow chains of indirections, don't evacuate them 
544     q = ((StgInd*)q)->indirectee;
545     goto loop;
546
547   case RET_BCO:
548   case RET_SMALL:
549   case RET_BIG:
550   case RET_DYN:
551   case UPDATE_FRAME:
552   case STOP_FRAME:
553   case CATCH_FRAME:
554   case CATCH_STM_FRAME:
555   case CATCH_RETRY_FRAME:
556   case ATOMICALLY_FRAME:
557     // shouldn't see these 
558     barf("evacuate: stack frame at %p\n", q);
559
560   case PAP:
561       return copy(q,pap_sizeW((StgPAP*)q),stp);
562
563   case AP:
564       return copy(q,ap_sizeW((StgAP*)q),stp);
565
566   case AP_STACK:
567       return copy(q,ap_stack_sizeW((StgAP_STACK*)q),stp);
568
569   case EVACUATED:
570     /* Already evacuated, just return the forwarding address.
571      * HOWEVER: if the requested destination generation (evac_gen) is
572      * older than the actual generation (because the object was
573      * already evacuated to a younger generation) then we have to
574      * set the failed_to_evac flag to indicate that we couldn't 
575      * manage to promote the object to the desired generation.
576      */
577     /* 
578      * Optimisation: the check is fairly expensive, but we can often
579      * shortcut it if either the required generation is 0, or the
580      * current object (the EVACUATED) is in a high enough generation.
581      * We know that an EVACUATED always points to an object in the
582      * same or an older generation.  stp is the lowest step that the
583      * current object would be evacuated to, so we only do the full
584      * check if stp is too low.
585      */
586     if (evac_gen > 0 && stp->gen_no < evac_gen) {  // optimisation 
587       StgClosure *p = ((StgEvacuated*)q)->evacuee;
588       if (HEAP_ALLOCED(p) && Bdescr((P_)p)->gen_no < evac_gen) {
589         failed_to_evac = rtsTrue;
590         TICK_GC_FAILED_PROMOTION();
591       }
592     }
593     return ((StgEvacuated*)q)->evacuee;
594
595   case ARR_WORDS:
596       // just copy the block 
597       return copy_noscav(q,arr_words_sizeW((StgArrWords *)q),stp);
598
599   case MUT_ARR_PTRS_CLEAN:
600   case MUT_ARR_PTRS_DIRTY:
601   case MUT_ARR_PTRS_FROZEN:
602   case MUT_ARR_PTRS_FROZEN0:
603       // just copy the block 
604       return copy(q,mut_arr_ptrs_sizeW((StgMutArrPtrs *)q),stp);
605
606   case TSO:
607     {
608       StgTSO *tso = (StgTSO *)q;
609
610       /* Deal with redirected TSOs (a TSO that's had its stack enlarged).
611        */
612       if (tso->what_next == ThreadRelocated) {
613         q = (StgClosure *)tso->link;
614         goto loop;
615       }
616
617       /* To evacuate a small TSO, we need to relocate the update frame
618        * list it contains.  
619        */
620       {
621           StgTSO *new_tso;
622           StgPtr p, q;
623
624           new_tso = (StgTSO *)copyPart((StgClosure *)tso,
625                                        tso_sizeW(tso),
626                                        sizeofW(StgTSO), stp);
627           move_TSO(tso, new_tso);
628           for (p = tso->sp, q = new_tso->sp;
629                p < tso->stack+tso->stack_size;) {
630               *q++ = *p++;
631           }
632           
633           return (StgClosure *)new_tso;
634       }
635     }
636
637   case TREC_HEADER: 
638     return copy(q,sizeofW(StgTRecHeader),stp);
639
640   case TVAR_WATCH_QUEUE:
641     return copy(q,sizeofW(StgTVarWatchQueue),stp);
642
643   case TVAR:
644     return copy(q,sizeofW(StgTVar),stp);
645     
646   case TREC_CHUNK:
647     return copy(q,sizeofW(StgTRecChunk),stp);
648
649   case ATOMIC_INVARIANT:
650     return copy(q,sizeofW(StgAtomicInvariant),stp);
651
652   case INVARIANT_CHECK_QUEUE:
653     return copy(q,sizeofW(StgInvariantCheckQueue),stp);
654
655   default:
656     barf("evacuate: strange closure type %d", (int)(info->type));
657   }
658
659   barf("evacuate");
660 }
661
662 /* -----------------------------------------------------------------------------
663    Evaluate a THUNK_SELECTOR if possible.
664
665    returns: NULL if we couldn't evaluate this THUNK_SELECTOR, or
666    a closure pointer if we evaluated it and this is the result.  Note
667    that "evaluating" the THUNK_SELECTOR doesn't necessarily mean
668    reducing it to HNF, just that we have eliminated the selection.
669    The result might be another thunk, or even another THUNK_SELECTOR.
670
671    If the return value is non-NULL, the original selector thunk has
672    been BLACKHOLE'd, and should be updated with an indirection or a
673    forwarding pointer.  If the return value is NULL, then the selector
674    thunk is unchanged.
675
676    ***
677    ToDo: the treatment of THUNK_SELECTORS could be improved in the
678    following way (from a suggestion by Ian Lynagh):
679
680    We can have a chain like this:
681
682       sel_0 --> (a,b)
683                  |
684                  |-----> sel_0 --> (a,b)
685                                     |
686                                     |-----> sel_0 --> ...
687
688    and the depth limit means we don't go all the way to the end of the
689    chain, which results in a space leak.  This affects the recursive
690    call to evacuate() in the THUNK_SELECTOR case in evacuate(): *not*
691    the recursive call to eval_thunk_selector() in
692    eval_thunk_selector().
693
694    We could eliminate the depth bound in this case, in the following
695    way:
696
697       - traverse the chain once to discover the *value* of the 
698         THUNK_SELECTOR.  Mark all THUNK_SELECTORS that we
699         visit on the way as having been visited already (somehow).
700
701       - in a second pass, traverse the chain again updating all
702         THUNK_SEELCTORS that we find on the way with indirections to
703         the value.
704
705       - if we encounter a "marked" THUNK_SELECTOR in a normal 
706         evacuate(), we konw it can't be updated so just evac it.
707
708    Program that illustrates the problem:
709
710         foo [] = ([], [])
711         foo (x:xs) = let (ys, zs) = foo xs
712                      in if x >= 0 then (x:ys, zs) else (ys, x:zs)
713
714         main = bar [1..(100000000::Int)]
715         bar xs = (\(ys, zs) -> print ys >> print zs) (foo xs)
716
717    -------------------------------------------------------------------------- */
718
719 static inline rtsBool
720 is_to_space ( StgClosure *p )
721 {
722     bdescr *bd;
723
724     bd = Bdescr((StgPtr)p);
725     if (HEAP_ALLOCED(p) &&
726         ((bd->flags & BF_EVACUATED) 
727          || ((bd->flags & BF_COMPACTED) &&
728              is_marked((P_)p,bd)))) {
729         return rtsTrue;
730     } else {
731         return rtsFalse;
732     }
733 }    
734
735 static StgClosure *
736 eval_thunk_selector( nat field, StgSelector * p )
737 {
738     StgInfoTable *info;
739     const StgInfoTable *info_ptr;
740     StgClosure *selectee;
741     
742     selectee = p->selectee;
743
744     // Save the real info pointer (NOTE: not the same as get_itbl()).
745     info_ptr = p->header.info;
746
747     // If the THUNK_SELECTOR is in a generation that we are not
748     // collecting, then bail out early.  We won't be able to save any
749     // space in any case, and updating with an indirection is trickier
750     // in an old gen.
751     if (Bdescr((StgPtr)p)->gen_no > N) {
752         return NULL;
753     }
754
755     // BLACKHOLE the selector thunk, since it is now under evaluation.
756     // This is important to stop us going into an infinite loop if
757     // this selector thunk eventually refers to itself.
758     SET_INFO(p,&stg_BLACKHOLE_info);
759
760 selector_loop:
761
762     // We don't want to end up in to-space, because this causes
763     // problems when the GC later tries to evacuate the result of
764     // eval_thunk_selector().  There are various ways this could
765     // happen:
766     //
767     // 1. following an IND_STATIC
768     //
769     // 2. when the old generation is compacted, the mark phase updates
770     //    from-space pointers to be to-space pointers, and we can't
771     //    reliably tell which we're following (eg. from an IND_STATIC).
772     // 
773     // 3. compacting GC again: if we're looking at a constructor in
774     //    the compacted generation, it might point directly to objects
775     //    in to-space.  We must bale out here, otherwise doing the selection
776     //    will result in a to-space pointer being returned.
777     //
778     //  (1) is dealt with using a BF_EVACUATED test on the
779     //  selectee. (2) and (3): we can tell if we're looking at an
780     //  object in the compacted generation that might point to
781     //  to-space objects by testing that (a) it is BF_COMPACTED, (b)
782     //  the compacted generation is being collected, and (c) the
783     //  object is marked.  Only a marked object may have pointers that
784     //  point to to-space objects, because that happens when
785     //  scavenging.
786     //
787     //  The to-space test is now embodied in the in_to_space() inline
788     //  function, as it is re-used below.
789     //
790     if (is_to_space(selectee)) {
791         goto bale_out;
792     }
793
794     info = get_itbl(selectee);
795     switch (info->type) {
796       case CONSTR:
797       case CONSTR_1_0:
798       case CONSTR_0_1:
799       case CONSTR_2_0:
800       case CONSTR_1_1:
801       case CONSTR_0_2:
802       case CONSTR_STATIC:
803       case CONSTR_NOCAF_STATIC:
804           // check that the size is in range 
805           ASSERT(field <  (StgWord32)(info->layout.payload.ptrs + 
806                                       info->layout.payload.nptrs));
807           
808           // Select the right field from the constructor, and check
809           // that the result isn't in to-space.  It might be in
810           // to-space if, for example, this constructor contains
811           // pointers to younger-gen objects (and is on the mut-once
812           // list).
813           //
814           { 
815               StgClosure *q;
816               q = selectee->payload[field];
817               if (is_to_space(q)) {
818                   goto bale_out;
819               } else {
820                   return q;
821               }
822           }
823
824       case IND:
825       case IND_PERM:
826       case IND_OLDGEN:
827       case IND_OLDGEN_PERM:
828       case IND_STATIC:
829           selectee = ((StgInd *)selectee)->indirectee;
830           goto selector_loop;
831
832       case EVACUATED:
833           // We don't follow pointers into to-space; the constructor
834           // has already been evacuated, so we won't save any space
835           // leaks by evaluating this selector thunk anyhow.
836           break;
837
838       case THUNK_SELECTOR:
839       {
840           StgClosure *val;
841
842           // check that we don't recurse too much, re-using the
843           // depth bound also used in evacuate().
844           if (thunk_selector_depth >= MAX_THUNK_SELECTOR_DEPTH) {
845               break;
846           }
847
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           if (Bdescr((P_)selectee)->flags && BF_COMPACTED) {
852               break;
853           }
854
855           thunk_selector_depth++;
856
857           val = eval_thunk_selector(info->layout.selector_offset, 
858                                     (StgSelector *)selectee);
859
860           thunk_selector_depth--;
861
862           if (val == NULL) { 
863               break;
864           } else {
865               // We evaluated this selector thunk, so update it with
866               // an indirection.  NOTE: we don't use UPD_IND here,
867               // because we are guaranteed that p is in a generation
868               // that we are collecting, and we never want to put the
869               // indirection on a mutable list.
870 #ifdef PROFILING
871               // For the purposes of LDV profiling, we have destroyed
872               // the original selector thunk.
873               SET_INFO(p, info_ptr);
874               LDV_RECORD_DEAD_FILL_SLOP_DYNAMIC(selectee);
875 #endif
876               ((StgInd *)selectee)->indirectee = val;
877               SET_INFO(selectee,&stg_IND_info);
878
879               // For the purposes of LDV profiling, we have created an
880               // indirection.
881               LDV_RECORD_CREATE(selectee);
882
883               selectee = val;
884               goto selector_loop;
885           }
886       }
887
888       case AP:
889       case AP_STACK:
890       case THUNK:
891       case THUNK_1_0:
892       case THUNK_0_1:
893       case THUNK_2_0:
894       case THUNK_1_1:
895       case THUNK_0_2:
896       case THUNK_STATIC:
897       case CAF_BLACKHOLE:
898       case SE_CAF_BLACKHOLE:
899       case SE_BLACKHOLE:
900       case BLACKHOLE:
901           // not evaluated yet 
902           break;
903     
904       default:
905         barf("eval_thunk_selector: strange selectee %d",
906              (int)(info->type));
907     }
908
909 bale_out:
910     // We didn't manage to evaluate this thunk; restore the old info pointer
911     SET_INFO(p, info_ptr);
912     return NULL;
913 }
914
915 /* -----------------------------------------------------------------------------
916    move_TSO is called to update the TSO structure after it has been
917    moved from one place to another.
918    -------------------------------------------------------------------------- */
919
920 void
921 move_TSO (StgTSO *src, StgTSO *dest)
922 {
923     ptrdiff_t diff;
924
925     // relocate the stack pointer... 
926     diff = (StgPtr)dest - (StgPtr)src; // In *words* 
927     dest->sp = (StgPtr)dest->sp + diff;
928 }
929