bbdba180c53cfdd804fa2009edf5ce354277d41f
[sbcl.git] / src / runtime / purify.c
1 /*
2  * C-level stuff to implement Lisp-level PURIFY
3  */
4
5 /*
6  * This software is part of the SBCL system. See the README file for
7  * more information.
8  *
9  * This software is derived from the CMU CL system, which was
10  * written at Carnegie Mellon University and released into the
11  * public domain. The software is in the public domain and is
12  * provided with absolutely no warranty. See the COPYING and CREDITS
13  * files for more information.
14  */
15
16 #include <stdio.h>
17 #include <sys/types.h>
18 #include <stdlib.h>
19
20 #include "runtime.h"
21 #include "os.h"
22 #include "sbcl.h"
23 #include "globals.h"
24 #include "validate.h"
25 #include "interrupt.h"
26 #include "purify.h"
27 #include "interr.h"
28 #ifdef GENCGC
29 #include "gencgc.h"
30 #endif
31
32 #undef PRINTNOISE
33
34 #if defined(ibmrt) || defined(__i386__)
35 static lispobj *dynamic_space_free_pointer;
36 #endif
37
38 #define gc_abort() \
39   lose("GC invariant lost, file \"%s\", line %d", __FILE__, __LINE__)
40
41 #if 1
42 #define gc_assert(ex) do { \
43         if (!(ex)) gc_abort(); \
44 } while (0)
45 #else
46 #define gc_assert(ex)
47 #endif
48
49 \f
50 /* These hold the original end of the read_only and static spaces so
51  * we can tell what are forwarding pointers. */
52
53 static lispobj *read_only_end, *static_end;
54
55 static lispobj *read_only_free, *static_free;
56
57 static lispobj *pscav(lispobj *addr, int nwords, boolean constant);
58
59 #define LATERBLOCKSIZE 1020
60 #define LATERMAXCOUNT 10
61
62 static struct later {
63     struct later *next;
64     union {
65         lispobj *ptr;
66         int count;
67     } u[LATERBLOCKSIZE];
68 } *later_blocks = NULL;
69 static int later_count = 0;
70
71 #define CEILING(x,y) (((x) + ((y) - 1)) & (~((y) - 1)))
72 #define NWORDS(x,y) (CEILING((x),(y)) / (y))
73
74 #ifdef sparc
75 #define RAW_ADDR_OFFSET 0
76 #else
77 #define RAW_ADDR_OFFSET (6*sizeof(lispobj) - type_FunctionPointer)
78 #endif
79 \f
80 static boolean
81 forwarding_pointer_p(lispobj obj)
82 {
83     lispobj *ptr;
84
85     ptr = (lispobj *)obj;
86
87     return ((static_end <= ptr && ptr <= static_free) ||
88             (read_only_end <= ptr && ptr <= read_only_free));
89 }
90
91 static boolean
92 dynamic_pointer_p(lispobj ptr)
93 {
94 #ifndef __i386__
95     /* KLUDGE: This has an implicit dependence on the ordering of
96      * address spaces, and is therefore basically wrong. I'd fix it,
97      * but I don't have a non-386 port to test it on. Porters are
98      * encouraged to fix it. -- WHN 2000-10-17 */
99     return (ptr >= (lispobj)DYNAMIC_SPACE_START);
100 #else
101     /* Be more conservative, and remember, this is a maybe. */
102     return (ptr >= (lispobj)DYNAMIC_SPACE_START
103             &&
104             ptr < (lispobj)dynamic_space_free_pointer);
105 #endif
106 }
107
108 \f
109 #ifdef __i386__
110
111 #ifdef WANT_CGC
112 /* original x86/CGC stack scavenging code by Paul Werkowski */
113
114 static int
115 maybe_can_move_p(lispobj thing)
116 {
117   lispobj *thingp,header;
118   if (dynamic_pointer_p(thing)) { /* in dynamic space */
119     thingp = (lispobj*)PTR(thing);
120     header = *thingp;
121     if(Pointerp(header) && forwarding_pointer_p(header))
122       return -1;                /* must change it */
123     if(LowtagOf(thing) == type_ListPointer)
124       return type_ListPointer;  /* can we check this somehow */
125     else if (thing & 3) {       /* not fixnum */
126       int kind = TypeOf(header);
127       /* printf(" %x %x",header,kind); */
128       switch (kind) {           /* something with a header */
129       case type_Bignum:
130       case type_SingleFloat:
131       case type_DoubleFloat:
132 #ifdef type_LongFloat
133       case type_LongFloat:
134 #endif
135       case type_Sap:
136       case type_SimpleVector:
137       case type_SimpleString:
138       case type_SimpleBitVector:
139       case type_SimpleArrayUnsignedByte2:
140       case type_SimpleArrayUnsignedByte4:
141       case type_SimpleArrayUnsignedByte8:
142       case type_SimpleArrayUnsignedByte16:
143       case type_SimpleArrayUnsignedByte32:
144 #ifdef type_SimpleArraySignedByte8
145       case type_SimpleArraySignedByte8:
146 #endif
147 #ifdef type_SimpleArraySignedByte16
148       case type_SimpleArraySignedByte16:
149 #endif
150 #ifdef type_SimpleArraySignedByte30
151       case type_SimpleArraySignedByte30:
152 #endif
153 #ifdef type_SimpleArraySignedByte32
154       case type_SimpleArraySignedByte32:
155 #endif
156       case type_SimpleArraySingleFloat:
157       case type_SimpleArrayDoubleFloat:
158 #ifdef type_SimpleArrayLongFloat
159       case type_SimpleArrayLongFloat:
160 #endif
161 #ifdef type_SimpleArrayComplexSingleFloat
162       case type_SimpleArrayComplexSingleFloat:
163 #endif
164 #ifdef type_SimpleArrayComplexDoubleFloat
165       case type_SimpleArrayComplexDoubleFloat:
166 #endif
167 #ifdef type_SimpleArrayComplexLongFloat
168       case type_SimpleArrayComplexLongFloat:
169 #endif
170       case type_CodeHeader:
171       case type_FunctionHeader:
172       case type_ClosureFunctionHeader:
173       case type_ReturnPcHeader:
174       case type_ClosureHeader:
175       case type_FuncallableInstanceHeader:
176       case type_InstanceHeader:
177       case type_ValueCellHeader:
178       case type_ByteCodeFunction:
179       case type_ByteCodeClosure:
180       case type_WeakPointer:
181       case type_Fdefn:
182         return kind;
183         break;
184       default:
185         return 0;
186       }}}
187   return 0;
188 }
189
190 static int pverbose=0;
191 #define PVERBOSE pverbose
192 static void
193 carefully_pscav_stack(lispobj*lowaddr, lispobj*base)
194 {
195   lispobj*sp = lowaddr;
196   while (sp < base)
197     { int k;
198       lispobj thing = *sp;
199       if((unsigned)thing & 0x3) /* may be pointer */
200         {
201           /* need to check for valid float/double? */
202           k = maybe_can_move_p(thing);
203           if(PVERBOSE)printf("%8x %8x %d\n",sp, thing, k);
204           if(k)
205             pscav(sp, 1, 0);
206         }
207       sp++;
208     }
209 }
210 #endif
211
212 #ifdef GENCGC
213 /*
214  * Enhanced x86/GENCGC stack scavenging by Douglas Crosher.
215  *
216  * Scavenging the stack on the i386 is problematic due to conservative
217  * roots and raw return addresses. Here it is handled in two passes:
218  * the first pass runs before any objects are moved and tries to
219  * identify valid pointers and return address on the stack, the second
220  * pass scavenges these.
221  */
222
223 static unsigned pointer_filter_verbose = 0;
224
225 static int
226 valid_dynamic_space_pointer(lispobj *pointer, lispobj *start_addr)
227 {
228   /* If it's not a return address then it needs to be a valid Lisp
229    * pointer. */
230   if (!Pointerp((lispobj)pointer))
231     return 0;
232
233   /* Check that the object pointed to is consistent with the pointer
234    * low tag. */
235   switch (LowtagOf((lispobj)pointer)) {
236   case type_FunctionPointer:
237     /* Start_addr should be the enclosing code object, or a closure
238      * header. */
239     switch (TypeOf(*start_addr)) {
240     case type_CodeHeader:
241       /* This case is probably caught above. */
242       break;
243     case type_ClosureHeader:
244     case type_FuncallableInstanceHeader:
245     case type_ByteCodeFunction:
246     case type_ByteCodeClosure:
247       if ((int)pointer != ((int)start_addr+type_FunctionPointer)) {
248         if (pointer_filter_verbose) {
249           fprintf(stderr,"*Wf2: %x %x %x\n", (unsigned int) pointer, 
250                   (unsigned int) start_addr, *start_addr);
251         }
252         return 0;
253       }
254       break;
255     default:
256       if (pointer_filter_verbose) {
257         fprintf(stderr,"*Wf3: %x %x %x\n", (unsigned int) pointer, 
258                 (unsigned int) start_addr, *start_addr);
259       }
260       return 0;
261     }
262     break;
263   case type_ListPointer:
264     if ((int)pointer != ((int)start_addr+type_ListPointer)) {
265       if (pointer_filter_verbose)
266         fprintf(stderr,"*Wl1: %x %x %x\n", (unsigned int) pointer, 
267                 (unsigned int) start_addr, *start_addr);
268       return 0;
269     }
270     /* Is it plausible cons? */
271     if((Pointerp(start_addr[0])
272         || ((start_addr[0] & 3) == 0) /* fixnum */
273         || (TypeOf(start_addr[0]) == type_BaseChar)
274         || (TypeOf(start_addr[0]) == type_UnboundMarker))
275        && (Pointerp(start_addr[1])
276            || ((start_addr[1] & 3) == 0) /* fixnum */
277            || (TypeOf(start_addr[1]) == type_BaseChar)
278            || (TypeOf(start_addr[1]) == type_UnboundMarker))) {
279       break;
280     } else {
281       if (pointer_filter_verbose) {
282         fprintf(stderr,"*Wl2: %x %x %x\n", (unsigned int) pointer, 
283                 (unsigned int) start_addr, *start_addr);
284       }
285       return 0;
286     }
287   case type_InstancePointer:
288     if ((int)pointer != ((int)start_addr+type_InstancePointer)) {
289       if (pointer_filter_verbose) {
290         fprintf(stderr,"*Wi1: %x %x %x\n", (unsigned int) pointer, 
291                 (unsigned int) start_addr, *start_addr);
292       }
293       return 0;
294     }
295     if (TypeOf(start_addr[0]) != type_InstanceHeader) {
296       if (pointer_filter_verbose) {
297         fprintf(stderr,"*Wi2: %x %x %x\n", (unsigned int) pointer, 
298                 (unsigned int) start_addr, *start_addr);
299       }
300       return 0;
301     }
302     break;
303   case type_OtherPointer:
304     if ((int)pointer != ((int)start_addr+type_OtherPointer)) {
305       if (pointer_filter_verbose) {
306         fprintf(stderr,"*Wo1: %x %x %x\n", (unsigned int) pointer, 
307                 (unsigned int) start_addr, *start_addr);
308       }
309       return 0;
310     }
311     /* Is it plausible?  Not a cons. X should check the headers. */
312     if(Pointerp(start_addr[0]) || ((start_addr[0] & 3) == 0)) {
313       if (pointer_filter_verbose) {
314         fprintf(stderr,"*Wo2: %x %x %x\n", (unsigned int) pointer, 
315                 (unsigned int) start_addr, *start_addr);
316       }
317       return 0;
318     }
319     switch (TypeOf(start_addr[0])) {
320     case type_UnboundMarker:
321     case type_BaseChar:
322       if (pointer_filter_verbose) {
323         fprintf(stderr,"*Wo3: %x %x %x\n", (unsigned int) pointer, 
324                 (unsigned int) start_addr, *start_addr);
325       }
326       return 0;
327
328       /* only pointed to by function pointers? */
329     case type_ClosureHeader:
330     case type_FuncallableInstanceHeader:
331     case type_ByteCodeFunction:
332     case type_ByteCodeClosure:
333       if (pointer_filter_verbose) {
334         fprintf(stderr,"*Wo4: %x %x %x\n", (unsigned int) pointer, 
335                 (unsigned int) start_addr, *start_addr);
336       }
337       return 0;
338
339     case type_InstanceHeader:
340       if (pointer_filter_verbose) {
341         fprintf(stderr,"*Wo5: %x %x %x\n", (unsigned int) pointer, 
342                 (unsigned int) start_addr, *start_addr);
343       }
344       return 0;
345
346       /* the valid other immediate pointer objects */
347     case type_SimpleVector:
348     case type_Ratio:
349     case type_Complex:
350 #ifdef type_ComplexSingleFloat
351     case type_ComplexSingleFloat:
352 #endif
353 #ifdef type_ComplexDoubleFloat
354     case type_ComplexDoubleFloat:
355 #endif
356 #ifdef type_ComplexLongFloat
357     case type_ComplexLongFloat:
358 #endif
359     case type_SimpleArray:
360     case type_ComplexString:
361     case type_ComplexBitVector:
362     case type_ComplexVector:
363     case type_ComplexArray:
364     case type_ValueCellHeader:
365     case type_SymbolHeader:
366     case type_Fdefn:
367     case type_CodeHeader:
368     case type_Bignum:
369     case type_SingleFloat:
370     case type_DoubleFloat:
371 #ifdef type_LongFloat
372     case type_LongFloat:
373 #endif
374     case type_SimpleString:
375     case type_SimpleBitVector:
376     case type_SimpleArrayUnsignedByte2:
377     case type_SimpleArrayUnsignedByte4:
378     case type_SimpleArrayUnsignedByte8:
379     case type_SimpleArrayUnsignedByte16:
380     case type_SimpleArrayUnsignedByte32:
381 #ifdef type_SimpleArraySignedByte8
382     case type_SimpleArraySignedByte8:
383 #endif
384 #ifdef type_SimpleArraySignedByte16
385     case type_SimpleArraySignedByte16:
386 #endif
387 #ifdef type_SimpleArraySignedByte30
388     case type_SimpleArraySignedByte30:
389 #endif
390 #ifdef type_SimpleArraySignedByte32
391     case type_SimpleArraySignedByte32:
392 #endif
393     case type_SimpleArraySingleFloat:
394     case type_SimpleArrayDoubleFloat:
395 #ifdef type_SimpleArrayLongFloat
396     case type_SimpleArrayLongFloat:
397 #endif
398 #ifdef type_SimpleArrayComplexSingleFloat
399     case type_SimpleArrayComplexSingleFloat:
400 #endif
401 #ifdef type_SimpleArrayComplexDoubleFloat
402     case type_SimpleArrayComplexDoubleFloat:
403 #endif
404 #ifdef type_SimpleArrayComplexLongFloat
405     case type_SimpleArrayComplexLongFloat:
406 #endif
407     case type_Sap:
408     case type_WeakPointer:
409       break;
410
411     default:
412       if (pointer_filter_verbose) {
413         fprintf(stderr,"*Wo6: %x %x %x\n", (unsigned int) pointer, 
414                 (unsigned int) start_addr, *start_addr);
415       }
416       return 0;
417     }
418     break;
419   default:
420     if (pointer_filter_verbose) {
421       fprintf(stderr,"*W?: %x %x %x\n", (unsigned int) pointer, 
422               (unsigned int) start_addr, *start_addr);
423     }
424     return 0;
425   }
426
427   /* looks good */
428   return 1;
429 }
430
431 #define MAX_STACK_POINTERS 256
432 lispobj *valid_stack_locations[MAX_STACK_POINTERS];
433 unsigned int num_valid_stack_locations;
434
435 #define MAX_STACK_RETURN_ADDRESSES 128
436 lispobj *valid_stack_ra_locations[MAX_STACK_RETURN_ADDRESSES];
437 lispobj *valid_stack_ra_code_objects[MAX_STACK_RETURN_ADDRESSES];
438 unsigned int num_valid_stack_ra_locations;
439
440 /* Identify valid stack slots. */
441 static void
442 setup_i386_stack_scav(lispobj *lowaddr, lispobj *base)
443 {
444   lispobj *sp = lowaddr;
445   num_valid_stack_locations = 0;
446   num_valid_stack_ra_locations = 0;
447   for (sp = lowaddr; sp < base; sp++) {
448     lispobj thing = *sp;
449     /* Find the object start address */
450     lispobj *start_addr = search_dynamic_space((void *)thing);
451     if (start_addr) {
452       /* We need to allow raw pointers into Code objects for return
453        * addresses. This will also pick up pointers to functions in code
454        * objects. */
455       if (TypeOf(*start_addr) == type_CodeHeader) {
456         gc_assert(num_valid_stack_ra_locations < MAX_STACK_RETURN_ADDRESSES);
457         valid_stack_ra_locations[num_valid_stack_ra_locations] = sp;
458         valid_stack_ra_code_objects[num_valid_stack_ra_locations++] =
459           (lispobj *)((int)start_addr + type_OtherPointer);
460       } else {
461         if (valid_dynamic_space_pointer((void *)thing, start_addr)) {
462           gc_assert(num_valid_stack_locations < MAX_STACK_POINTERS);
463           valid_stack_locations[num_valid_stack_locations++] = sp;
464         }
465       }
466     }
467   }
468   if (pointer_filter_verbose) {
469     fprintf(stderr, "number of valid stack pointers = %d\n",
470             num_valid_stack_locations);
471     fprintf(stderr, "number of stack return addresses = %d\n",
472             num_valid_stack_ra_locations);
473   }
474 }
475
476 static void
477 pscav_i386_stack(void)
478 {
479   int i;
480
481   for (i = 0; i < num_valid_stack_locations; i++)
482     pscav(valid_stack_locations[i], 1, 0);
483
484   for (i = 0; i < num_valid_stack_ra_locations; i++) {
485     lispobj code_obj = (lispobj)valid_stack_ra_code_objects[i];
486     pscav(&code_obj, 1, 0);
487     if (pointer_filter_verbose) {
488       fprintf(stderr,"*C moved RA %x to %x; for code object %x to %x\n",
489               *valid_stack_ra_locations[i],
490               (int)(*valid_stack_ra_locations[i])
491               - ((int)valid_stack_ra_code_objects[i] - (int)code_obj),
492               (unsigned int) valid_stack_ra_code_objects[i], code_obj);
493     }
494     *valid_stack_ra_locations[i] =
495       ((int)(*valid_stack_ra_locations[i])
496        - ((int)valid_stack_ra_code_objects[i] - (int)code_obj));
497   }
498 }
499 #endif
500 #endif
501
502 \f
503 static void
504 pscav_later(lispobj *where, int count)
505 {
506     struct later *new;
507
508     if (count > LATERMAXCOUNT) {
509         while (count > LATERMAXCOUNT) {
510             pscav_later(where, LATERMAXCOUNT);
511             count -= LATERMAXCOUNT;
512             where += LATERMAXCOUNT;
513         }
514     }
515     else {
516         if (later_blocks == NULL || later_count == LATERBLOCKSIZE ||
517             (later_count == LATERBLOCKSIZE-1 && count > 1)) {
518             new  = (struct later *)malloc(sizeof(struct later));
519             new->next = later_blocks;
520             if (later_blocks && later_count < LATERBLOCKSIZE)
521                 later_blocks->u[later_count].ptr = NULL;
522             later_blocks = new;
523             later_count = 0;
524         }
525
526         if (count != 1)
527             later_blocks->u[later_count++].count = count;
528         later_blocks->u[later_count++].ptr = where;
529     }
530 }
531
532 static lispobj ptrans_boxed(lispobj thing, lispobj header, boolean constant)
533 {
534     int nwords;
535     lispobj result, *new, *old;
536
537     nwords = 1 + HeaderValue(header);
538
539     /* Allocate it */
540     old = (lispobj *)PTR(thing);
541     if (constant) {
542         new = read_only_free;
543         read_only_free += CEILING(nwords, 2);
544     }
545     else {
546         new = static_free;
547         static_free += CEILING(nwords, 2);
548     }
549
550     /* Copy it. */
551     bcopy(old, new, nwords * sizeof(lispobj));
552
553     /* Deposit forwarding pointer. */
554     result = (lispobj)new | LowtagOf(thing);
555     *old = result;
556
557     /* Scavenge it. */
558     pscav(new, nwords, constant);
559
560     return result;
561 }
562
563 /* We need to look at the layout to see whether it is a pure structure
564  * class, and only then can we transport as constant. If it is pure, we can
565  * ALWAYS transport as a constant. */
566 static lispobj ptrans_instance(lispobj thing, lispobj header, boolean constant)
567 {
568     lispobj layout = ((struct instance *)PTR(thing))->slots[0];
569     lispobj pure = ((struct instance *)PTR(layout))->slots[15];
570
571     switch (pure) {
572     case T:
573         return (ptrans_boxed(thing, header, 1));
574     case NIL:
575         return (ptrans_boxed(thing, header, 0));
576     case 0:
577         {
578             /* Substructure: special case for the COMPACT-INFO-ENVs, where
579              * the instance may have a point to the dynamic space placed
580              * into it (e.g. the cache-name slot), but the lists and arrays
581              * at the time of a purify can be moved to the RO space. */
582             int nwords;
583             lispobj result, *new, *old;
584
585             nwords = 1 + HeaderValue(header);
586
587             /* Allocate it */
588             old = (lispobj *)PTR(thing);
589             new = static_free;
590             static_free += CEILING(nwords, 2);
591
592             /* Copy it. */
593             bcopy(old, new, nwords * sizeof(lispobj));
594
595             /* Deposit forwarding pointer. */
596             result = (lispobj)new | LowtagOf(thing);
597             *old = result;
598
599             /* Scavenge it. */
600             pscav(new, nwords, 1);
601
602             return result;
603         }
604     default:
605         gc_abort();
606         return NIL; /* dummy value: return something ... */
607     }
608 }
609
610 static lispobj ptrans_fdefn(lispobj thing, lispobj header)
611 {
612     int nwords;
613     lispobj result, *new, *old, oldfn;
614     struct fdefn *fdefn;
615
616     nwords = 1 + HeaderValue(header);
617
618     /* Allocate it */
619     old = (lispobj *)PTR(thing);
620     new = static_free;
621     static_free += CEILING(nwords, 2);
622
623     /* Copy it. */
624     bcopy(old, new, nwords * sizeof(lispobj));
625
626     /* Deposit forwarding pointer. */
627     result = (lispobj)new | LowtagOf(thing);
628     *old = result;
629
630     /* Scavenge the function. */
631     fdefn = (struct fdefn *)new;
632     oldfn = fdefn->function;
633     pscav(&fdefn->function, 1, 0);
634     if ((char *)oldfn + RAW_ADDR_OFFSET == fdefn->raw_addr)
635         fdefn->raw_addr = (char *)fdefn->function + RAW_ADDR_OFFSET;
636
637     return result;
638 }
639
640 static lispobj ptrans_unboxed(lispobj thing, lispobj header)
641 {
642     int nwords;
643     lispobj result, *new, *old;
644
645     nwords = 1 + HeaderValue(header);
646
647     /* Allocate it */
648     old = (lispobj *)PTR(thing);
649     new = read_only_free;
650     read_only_free += CEILING(nwords, 2);
651
652     /* Copy it. */
653     bcopy(old, new, nwords * sizeof(lispobj));
654
655     /* Deposit forwarding pointer. */
656     result = (lispobj)new | LowtagOf(thing);
657     *old = result;
658
659     return result;
660 }
661
662 static lispobj ptrans_vector(lispobj thing, int bits, int extra,
663                              boolean boxed, boolean constant)
664 {
665     struct vector *vector;
666     int nwords;
667     lispobj result, *new;
668
669     vector = (struct vector *)PTR(thing);
670     nwords = 2 + (CEILING((fixnum_value(vector->length)+extra)*bits,32)>>5);
671
672     if (boxed && !constant) {
673         new = static_free;
674         static_free += CEILING(nwords, 2);
675     }
676     else {
677         new = read_only_free;
678         read_only_free += CEILING(nwords, 2);
679     }
680
681     bcopy(vector, new, nwords * sizeof(lispobj));
682
683     result = (lispobj)new | LowtagOf(thing);
684     vector->header = result;
685
686     if (boxed)
687         pscav(new, nwords, constant);
688
689     return result;
690 }
691
692 #ifdef __i386__
693 static void
694 apply_code_fixups_during_purify(struct code *old_code, struct code *new_code)
695 {
696   int nheader_words, ncode_words, nwords;
697   void  *constants_start_addr, *constants_end_addr;
698   void  *code_start_addr, *code_end_addr;
699   lispobj fixups = NIL;
700   unsigned  displacement = (unsigned)new_code - (unsigned)old_code;
701   struct vector *fixups_vector;
702
703   /* Byte compiled code has no fixups. The trace table offset will be
704    * a fixnum if it's x86 compiled code - check. */
705   if (new_code->trace_table_offset & 0x3)
706     return;
707
708   /* Else it's x86 machine code. */
709   ncode_words = fixnum_value(new_code->code_size);
710   nheader_words = HeaderValue(*(lispobj *)new_code);
711   nwords = ncode_words + nheader_words;
712
713   constants_start_addr = (void *)new_code + 5*4;
714   constants_end_addr = (void *)new_code + nheader_words*4;
715   code_start_addr = (void *)new_code + nheader_words*4;
716   code_end_addr = (void *)new_code + nwords*4;
717
718   /* The first constant should be a pointer to the fixups for this
719    * code objects. Check. */
720   fixups = new_code->constants[0];
721
722   /* It will be 0 or the unbound-marker if there are no fixups, and
723    * will be an other-pointer to a vector if it is valid. */
724   if ((fixups==0) || (fixups==type_UnboundMarker) || !Pointerp(fixups)) {
725 #ifdef GENCGC
726     /* Check for a possible errors. */
727     sniff_code_object(new_code,displacement);
728 #endif
729     return;
730   }
731
732   fixups_vector = (struct vector *)PTR(fixups);
733
734   /* Could be pointing to a forwarding pointer. */
735   if (Pointerp(fixups) && (dynamic_pointer_p(fixups))
736       && forwarding_pointer_p(*(lispobj *)fixups_vector)) {
737     /* If so then follow it. */
738     fixups_vector = (struct vector *)PTR(*(lispobj *)fixups_vector);
739   }
740
741   if (TypeOf(fixups_vector->header) == type_SimpleArrayUnsignedByte32) {
742     /* We got the fixups for the code block. Now work through the vector,
743      * and apply a fixup at each address. */
744     int length = fixnum_value(fixups_vector->length);
745     int i;
746     for (i=0; i<length; i++) {
747       unsigned offset = fixups_vector->data[i];
748       /* Now check the current value of offset. */
749       unsigned  old_value = *(unsigned *)((unsigned)code_start_addr + offset);
750
751       /* If it's within the old_code object then it must be an
752        * absolute fixup (relative ones are not saved) */
753       if ((old_value>=(unsigned)old_code)
754           && (old_value<((unsigned)old_code + nwords*4)))
755         /* So add the dispacement. */
756         *(unsigned *)((unsigned)code_start_addr + offset) = old_value
757           + displacement;
758       else
759         /* It is outside the old code object so it must be a relative
760          * fixup (absolute fixups are not saved). So subtract the
761          * displacement. */
762         *(unsigned *)((unsigned)code_start_addr + offset) = old_value
763           - displacement;
764     }
765   }
766
767   /* No longer need the fixups. */
768   new_code->constants[0] = 0;
769
770 #ifdef GENCGC
771   /* Check for possible errors. */
772   sniff_code_object(new_code,displacement);
773 #endif
774 }
775 #endif
776
777 static lispobj ptrans_code(lispobj thing)
778 {
779     struct code *code, *new;
780     int nwords;
781     lispobj func, result;
782
783     code = (struct code *)PTR(thing);
784     nwords = HeaderValue(code->header) + fixnum_value(code->code_size);
785
786     new = (struct code *)read_only_free;
787     read_only_free += CEILING(nwords, 2);
788
789     bcopy(code, new, nwords * sizeof(lispobj));
790
791 #ifdef __i386__
792     apply_code_fixups_during_purify(code,new);
793 #endif
794
795     result = (lispobj)new | type_OtherPointer;
796
797     /* Stick in a forwarding pointer for the code object. */
798     *(lispobj *)code = result;
799
800     /* Put in forwarding pointers for all the functions. */
801     for (func = code->entry_points;
802          func != NIL;
803          func = ((struct function *)PTR(func))->next) {
804
805         gc_assert(LowtagOf(func) == type_FunctionPointer);
806
807         *(lispobj *)PTR(func) = result + (func - thing);
808     }
809
810     /* Arrange to scavenge the debug info later. */
811     pscav_later(&new->debug_info, 1);
812
813     if(new->trace_table_offset & 0x3)
814 #if 0
815       pscav(&new->trace_table_offset, 1, 0);
816 #else
817       new->trace_table_offset = NIL; /* limit lifetime */
818 #endif
819
820     /* Scavenge the constants. */
821     pscav(new->constants, HeaderValue(new->header)-5, 1);
822
823     /* Scavenge all the functions. */
824     pscav(&new->entry_points, 1, 1);
825     for (func = new->entry_points;
826          func != NIL;
827          func = ((struct function *)PTR(func))->next) {
828         gc_assert(LowtagOf(func) == type_FunctionPointer);
829         gc_assert(!dynamic_pointer_p(func));
830
831 #ifdef __i386__
832         /* Temporarly convert the self pointer to a real function
833            pointer. */
834         ((struct function *)PTR(func))->self -= RAW_ADDR_OFFSET;
835 #endif
836         pscav(&((struct function *)PTR(func))->self, 2, 1);
837 #ifdef __i386__
838         ((struct function *)PTR(func))->self += RAW_ADDR_OFFSET;
839 #endif
840         pscav_later(&((struct function *)PTR(func))->name, 3);
841     }
842
843     return result;
844 }
845
846 static lispobj ptrans_func(lispobj thing, lispobj header)
847 {
848     int nwords;
849     lispobj code, *new, *old, result;
850     struct function *function;
851
852     /* Thing can either be a function header, a closure function
853      * header, a closure, or a funcallable-instance. If it's a closure
854      * or a funcallable-instance, we do the same as ptrans_boxed.
855      * Otherwise we have to do something strange, 'cause it is buried
856      * inside a code object. */
857
858     if (TypeOf(header) == type_FunctionHeader ||
859         TypeOf(header) == type_ClosureFunctionHeader) {
860
861         /* We can only end up here if the code object has not been
862          * scavenged, because if it had been scavenged, forwarding pointers
863          * would have been left behind for all the entry points. */
864
865         function = (struct function *)PTR(thing);
866         code = (PTR(thing)-(HeaderValue(function->header)*sizeof(lispobj))) |
867             type_OtherPointer;
868
869         /* This will cause the function's header to be replaced with a 
870          * forwarding pointer. */
871         ptrans_code(code);
872
873         /* So we can just return that. */
874         return function->header;
875     }
876     else {
877         /* It's some kind of closure-like thing. */
878         nwords = 1 + HeaderValue(header);
879         old = (lispobj *)PTR(thing);
880
881         /* Allocate the new one. */
882         if (TypeOf(header) == type_FuncallableInstanceHeader) {
883             /* FINs *must* not go in read_only space. */
884             new = static_free;
885             static_free += CEILING(nwords, 2);
886         }
887         else {
888             /* Closures can always go in read-only space, 'cause they
889              * never change. */
890
891             new = read_only_free;
892             read_only_free += CEILING(nwords, 2);
893         }
894         /* Copy it. */
895         bcopy(old, new, nwords * sizeof(lispobj));
896
897         /* Deposit forwarding pointer. */
898         result = (lispobj)new | LowtagOf(thing);
899         *old = result;
900
901         /* Scavenge it. */
902         pscav(new, nwords, 0);
903
904         return result;
905     }
906 }
907
908 static lispobj ptrans_returnpc(lispobj thing, lispobj header)
909 {
910     lispobj code, new;
911
912     /* Find the corresponding code object. */
913     code = thing - HeaderValue(header)*sizeof(lispobj);
914
915     /* Make sure it's been transported. */
916     new = *(lispobj *)PTR(code);
917     if (!forwarding_pointer_p(new))
918         new = ptrans_code(code);
919
920     /* Maintain the offset: */
921     return new + (thing - code);
922 }
923
924 #define WORDS_PER_CONS CEILING(sizeof(struct cons) / sizeof(lispobj), 2)
925
926 static lispobj ptrans_list(lispobj thing, boolean constant)
927 {
928     struct cons *old, *new, *orig;
929     int length;
930
931     if (constant)
932         orig = (struct cons *)read_only_free;
933     else
934         orig = (struct cons *)static_free;
935     length = 0;
936
937     do {
938         /* Allocate a new cons cell. */
939         old = (struct cons *)PTR(thing);
940         if (constant) {
941             new = (struct cons *)read_only_free;
942             read_only_free += WORDS_PER_CONS;
943         }
944         else {
945             new = (struct cons *)static_free;
946             static_free += WORDS_PER_CONS;
947         }
948
949         /* Copy the cons cell and keep a pointer to the cdr. */
950         new->car = old->car;
951         thing = new->cdr = old->cdr;
952
953         /* Set up the forwarding pointer. */
954         *(lispobj *)old = ((lispobj)new) | type_ListPointer;
955
956         /* And count this cell. */
957         length++;
958     } while (LowtagOf(thing) == type_ListPointer &&
959              dynamic_pointer_p(thing) &&
960              !(forwarding_pointer_p(*(lispobj *)PTR(thing))));
961
962     /* Scavenge the list we just copied. */
963     pscav((lispobj *)orig, length * WORDS_PER_CONS, constant);
964
965     return ((lispobj)orig) | type_ListPointer;
966 }
967
968 static lispobj ptrans_otherptr(lispobj thing, lispobj header, boolean constant)
969 {
970     switch (TypeOf(header)) {
971       case type_Bignum:
972       case type_SingleFloat:
973       case type_DoubleFloat:
974 #ifdef type_LongFloat
975       case type_LongFloat:
976 #endif
977 #ifdef type_ComplexSingleFloat
978       case type_ComplexSingleFloat:
979 #endif
980 #ifdef type_ComplexDoubleFloat
981       case type_ComplexDoubleFloat:
982 #endif
983 #ifdef type_ComplexLongFloat
984       case type_ComplexLongFloat:
985 #endif
986       case type_Sap:
987         return ptrans_unboxed(thing, header);
988
989       case type_Ratio:
990       case type_Complex:
991       case type_SimpleArray:
992       case type_ComplexString:
993       case type_ComplexVector:
994       case type_ComplexArray:
995         return ptrans_boxed(thing, header, constant);
996         
997       case type_ValueCellHeader:
998       case type_WeakPointer:
999         return ptrans_boxed(thing, header, 0);
1000
1001       case type_SymbolHeader:
1002         return ptrans_boxed(thing, header, 0);
1003
1004       case type_SimpleString:
1005         return ptrans_vector(thing, 8, 1, 0, constant);
1006
1007       case type_SimpleBitVector:
1008         return ptrans_vector(thing, 1, 0, 0, constant);
1009
1010       case type_SimpleVector:
1011         return ptrans_vector(thing, 32, 0, 1, constant);
1012
1013       case type_SimpleArrayUnsignedByte2:
1014         return ptrans_vector(thing, 2, 0, 0, constant);
1015
1016       case type_SimpleArrayUnsignedByte4:
1017         return ptrans_vector(thing, 4, 0, 0, constant);
1018
1019       case type_SimpleArrayUnsignedByte8:
1020 #ifdef type_SimpleArraySignedByte8
1021       case type_SimpleArraySignedByte8:
1022 #endif
1023         return ptrans_vector(thing, 8, 0, 0, constant);
1024
1025       case type_SimpleArrayUnsignedByte16:
1026 #ifdef type_SimpleArraySignedByte16
1027       case type_SimpleArraySignedByte16:
1028 #endif
1029         return ptrans_vector(thing, 16, 0, 0, constant);
1030
1031       case type_SimpleArrayUnsignedByte32:
1032 #ifdef type_SimpleArraySignedByte30
1033       case type_SimpleArraySignedByte30:
1034 #endif
1035 #ifdef type_SimpleArraySignedByte32
1036       case type_SimpleArraySignedByte32:
1037 #endif
1038         return ptrans_vector(thing, 32, 0, 0, constant);
1039
1040       case type_SimpleArraySingleFloat:
1041         return ptrans_vector(thing, 32, 0, 0, constant);
1042
1043       case type_SimpleArrayDoubleFloat:
1044         return ptrans_vector(thing, 64, 0, 0, constant);
1045
1046 #ifdef type_SimpleArrayLongFloat
1047       case type_SimpleArrayLongFloat:
1048 #ifdef __i386__
1049         return ptrans_vector(thing, 96, 0, 0, constant);
1050 #endif
1051 #ifdef sparc
1052         return ptrans_vector(thing, 128, 0, 0, constant);
1053 #endif
1054 #endif
1055
1056 #ifdef type_SimpleArrayComplexSingleFloat
1057       case type_SimpleArrayComplexSingleFloat:
1058         return ptrans_vector(thing, 64, 0, 0, constant);
1059 #endif
1060
1061 #ifdef type_SimpleArrayComplexDoubleFloat
1062       case type_SimpleArrayComplexDoubleFloat:
1063         return ptrans_vector(thing, 128, 0, 0, constant);
1064 #endif
1065
1066 #ifdef type_SimpleArrayComplexLongFloat
1067       case type_SimpleArrayComplexLongFloat:
1068 #ifdef __i386__
1069         return ptrans_vector(thing, 192, 0, 0, constant);
1070 #endif
1071 #ifdef sparc
1072         return ptrans_vector(thing, 256, 0, 0, constant);
1073 #endif
1074 #endif
1075
1076       case type_CodeHeader:
1077         return ptrans_code(thing);
1078
1079       case type_ReturnPcHeader:
1080         return ptrans_returnpc(thing, header);
1081
1082       case type_Fdefn:
1083         return ptrans_fdefn(thing, header);
1084
1085       default:
1086         /* Should only come across other pointers to the above stuff. */
1087         gc_abort();
1088         return NIL;
1089     }
1090 }
1091
1092 static int pscav_fdefn(struct fdefn *fdefn)
1093 {
1094     boolean fix_func;
1095
1096     fix_func = ((char *)(fdefn->function+RAW_ADDR_OFFSET) == fdefn->raw_addr);
1097     pscav(&fdefn->name, 1, 1);
1098     pscav(&fdefn->function, 1, 0);
1099     if (fix_func)
1100         fdefn->raw_addr = (char *)(fdefn->function + RAW_ADDR_OFFSET);
1101     return sizeof(struct fdefn) / sizeof(lispobj);
1102 }
1103
1104 #ifdef __i386__
1105 /* now putting code objects in static space */
1106 static int
1107 pscav_code(struct code*code)
1108 {
1109     int nwords;
1110     lispobj func;
1111     nwords = HeaderValue(code->header) + fixnum_value(code->code_size);
1112
1113     /* pw--The trace_table_offset slot can contain a list pointer. This
1114      * occurs when the code object is a top level form that initializes
1115      * a byte-compiled function. The fact that PURIFY was ignoring this
1116      * slot may be a bug unrelated to the x86 port, except that TLF's
1117      * normally become unreachable after the loader calls them and
1118      * won't be seen by PURIFY at all!! */
1119     if(code->trace_table_offset & 0x3)
1120 #if 0
1121       pscav(&code->trace_table_offset, 1, 0);
1122 #else
1123       code->trace_table_offset = NIL; /* limit lifetime */
1124 #endif
1125
1126     /* Arrange to scavenge the debug info later. */
1127     pscav_later(&code->debug_info, 1);
1128
1129     /* Scavenge the constants. */
1130     pscav(code->constants, HeaderValue(code->header)-5, 1);
1131
1132     /* Scavenge all the functions. */
1133     pscav(&code->entry_points, 1, 1);
1134     for (func = code->entry_points;
1135          func != NIL;
1136          func = ((struct function *)PTR(func))->next) {
1137         gc_assert(LowtagOf(func) == type_FunctionPointer);
1138         gc_assert(!dynamic_pointer_p(func));
1139
1140 #ifdef __i386__
1141         /* Temporarly convert the self pointer to a real function
1142          * pointer. */
1143         ((struct function *)PTR(func))->self -= RAW_ADDR_OFFSET;
1144 #endif
1145         pscav(&((struct function *)PTR(func))->self, 2, 1);
1146 #ifdef __i386__
1147         ((struct function *)PTR(func))->self += RAW_ADDR_OFFSET;
1148 #endif
1149         pscav_later(&((struct function *)PTR(func))->name, 3);
1150     }
1151
1152     return CEILING(nwords,2);
1153 }
1154 #endif
1155
1156 static lispobj *pscav(lispobj *addr, int nwords, boolean constant)
1157 {
1158     lispobj thing, *thingp, header;
1159     int count = 0; /* (0 = dummy init value to stop GCC warning) */
1160     struct vector *vector;
1161
1162     while (nwords > 0) {
1163         thing = *addr;
1164         if (Pointerp(thing)) {
1165             /* It's a pointer. Is it something we might have to move? */
1166             if (dynamic_pointer_p(thing)) {
1167                 /* Maybe. Have we already moved it? */
1168                 thingp = (lispobj *)PTR(thing);
1169                 header = *thingp;
1170                 if (Pointerp(header) && forwarding_pointer_p(header))
1171                     /* Yep, so just copy the forwarding pointer. */
1172                     thing = header;
1173                 else {
1174                     /* Nope, copy the object. */
1175                     switch (LowtagOf(thing)) {
1176                       case type_FunctionPointer:
1177                         thing = ptrans_func(thing, header);
1178                         break;
1179
1180                       case type_ListPointer:
1181                         thing = ptrans_list(thing, constant);
1182                         break;
1183
1184                       case type_InstancePointer:
1185                         thing = ptrans_instance(thing, header, constant);
1186                         break;
1187
1188                       case type_OtherPointer:
1189                         thing = ptrans_otherptr(thing, header, constant);
1190                         break;
1191
1192                       default:
1193                         /* It was a pointer, but not one of them? */
1194                         gc_abort();
1195                     }
1196                 }
1197                 *addr = thing;
1198             }
1199             count = 1;
1200         }
1201         else if (thing & 3) {
1202             /* It's an other immediate. Maybe the header for an unboxed */
1203             /* object. */
1204             switch (TypeOf(thing)) {
1205               case type_Bignum:
1206               case type_SingleFloat:
1207               case type_DoubleFloat:
1208 #ifdef type_LongFloat
1209               case type_LongFloat:
1210 #endif
1211               case type_Sap:
1212                 /* It's an unboxed simple object. */
1213                 count = HeaderValue(thing)+1;
1214                 break;
1215
1216               case type_SimpleVector:
1217                 if (HeaderValue(thing) == subtype_VectorValidHashing)
1218                     *addr = (subtype_VectorMustRehash<<type_Bits) |
1219                         type_SimpleVector;
1220                 count = 1;
1221                 break;
1222
1223               case type_SimpleString:
1224                 vector = (struct vector *)addr;
1225                 count = CEILING(NWORDS(fixnum_value(vector->length)+1,4)+2,2);
1226                 break;
1227
1228               case type_SimpleBitVector:
1229                 vector = (struct vector *)addr;
1230                 count = CEILING(NWORDS(fixnum_value(vector->length),32)+2,2);
1231                 break;
1232
1233               case type_SimpleArrayUnsignedByte2:
1234                 vector = (struct vector *)addr;
1235                 count = CEILING(NWORDS(fixnum_value(vector->length),16)+2,2);
1236                 break;
1237
1238               case type_SimpleArrayUnsignedByte4:
1239                 vector = (struct vector *)addr;
1240                 count = CEILING(NWORDS(fixnum_value(vector->length),8)+2,2);
1241                 break;
1242
1243               case type_SimpleArrayUnsignedByte8:
1244 #ifdef type_SimpleArraySignedByte8
1245               case type_SimpleArraySignedByte8:
1246 #endif
1247                 vector = (struct vector *)addr;
1248                 count = CEILING(NWORDS(fixnum_value(vector->length),4)+2,2);
1249                 break;
1250
1251               case type_SimpleArrayUnsignedByte16:
1252 #ifdef type_SimpleArraySignedByte16
1253               case type_SimpleArraySignedByte16:
1254 #endif
1255                 vector = (struct vector *)addr;
1256                 count = CEILING(NWORDS(fixnum_value(vector->length),2)+2,2);
1257                 break;
1258
1259               case type_SimpleArrayUnsignedByte32:
1260 #ifdef type_SimpleArraySignedByte30
1261               case type_SimpleArraySignedByte30:
1262 #endif
1263 #ifdef type_SimpleArraySignedByte32
1264               case type_SimpleArraySignedByte32:
1265 #endif
1266                 vector = (struct vector *)addr;
1267                 count = CEILING(fixnum_value(vector->length)+2,2);
1268                 break;
1269
1270               case type_SimpleArraySingleFloat:
1271                 vector = (struct vector *)addr;
1272                 count = CEILING(fixnum_value(vector->length)+2,2);
1273                 break;
1274
1275               case type_SimpleArrayDoubleFloat:
1276 #ifdef type_SimpleArrayComplexSingleFloat
1277               case type_SimpleArrayComplexSingleFloat:
1278 #endif
1279                 vector = (struct vector *)addr;
1280                 count = fixnum_value(vector->length)*2+2;
1281                 break;
1282
1283 #ifdef type_SimpleArrayLongFloat
1284               case type_SimpleArrayLongFloat:
1285                 vector = (struct vector *)addr;
1286 #ifdef __i386__
1287                 count = fixnum_value(vector->length)*3+2;
1288 #endif
1289 #ifdef sparc
1290                 count = fixnum_value(vector->length)*4+2;
1291 #endif
1292                 break;
1293 #endif
1294
1295 #ifdef type_SimpleArrayComplexDoubleFloat
1296               case type_SimpleArrayComplexDoubleFloat:
1297                 vector = (struct vector *)addr;
1298                 count = fixnum_value(vector->length)*4+2;
1299                 break;
1300 #endif
1301
1302 #ifdef type_SimpleArrayComplexLongFloat
1303               case type_SimpleArrayComplexLongFloat:
1304                 vector = (struct vector *)addr;
1305 #ifdef __i386__
1306                 count = fixnum_value(vector->length)*6+2;
1307 #endif
1308 #ifdef sparc
1309                 count = fixnum_value(vector->length)*8+2;
1310 #endif
1311                 break;
1312 #endif
1313
1314               case type_CodeHeader:
1315 #ifndef __i386__
1316                 gc_abort(); /* no code headers in static space */
1317 #else
1318                 count = pscav_code((struct code*)addr);
1319 #endif
1320                 break;
1321
1322               case type_FunctionHeader:
1323               case type_ClosureFunctionHeader:
1324               case type_ReturnPcHeader:
1325                 /* We should never hit any of these, 'cause they occur
1326                  * buried in the middle of code objects. */
1327                 gc_abort();
1328                 break;
1329
1330 #ifdef __i386__
1331               case type_ClosureHeader:
1332               case type_FuncallableInstanceHeader:
1333               case type_ByteCodeFunction:
1334               case type_ByteCodeClosure:
1335                 /* The function self pointer needs special care on the
1336                  * x86 because it is the real entry point. */
1337                 {
1338                   lispobj fun = ((struct closure *)addr)->function
1339                     - RAW_ADDR_OFFSET;
1340                   pscav(&fun, 1, constant);
1341                   ((struct closure *)addr)->function = fun + RAW_ADDR_OFFSET;
1342                 }
1343                 count = 2;
1344                 break;
1345 #endif
1346
1347               case type_WeakPointer:
1348                 /* Weak pointers get preserved during purify, 'cause I
1349                  * don't feel like figuring out how to break them. */
1350                 pscav(addr+1, 2, constant);
1351                 count = 4;
1352                 break;
1353
1354               case type_Fdefn:
1355                 /* We have to handle fdefn objects specially, so we
1356                  * can fix up the raw function address. */
1357                 count = pscav_fdefn((struct fdefn *)addr);
1358                 break;
1359
1360               default:
1361                 count = 1;
1362                 break;
1363             }
1364         }
1365         else {
1366             /* It's a fixnum. */
1367             count = 1;
1368         }
1369
1370         addr += count;
1371         nwords -= count;
1372     }
1373
1374     return addr;
1375 }
1376
1377 int purify(lispobj static_roots, lispobj read_only_roots)
1378 {
1379     lispobj *clean;
1380     int count, i;
1381     struct later *laters, *next;
1382
1383 #ifdef PRINTNOISE
1384     printf("[doing purification:");
1385     fflush(stdout);
1386 #endif
1387
1388     if (fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX)) != 0) {
1389         /* FIXME: 1. What does this mean? 2. It shouldn't be reporting
1390          * its error simply by a. printing a string b. to stdout instead
1391          * of stderr. */
1392         printf(" Ack! Can't purify interrupt contexts. ");
1393         fflush(stdout);
1394         return 0;
1395     }
1396
1397 #if defined(ibmrt) || defined(__i386__)
1398     dynamic_space_free_pointer =
1399       (lispobj*)SymbolValue(ALLOCATION_POINTER);
1400 #endif
1401
1402     read_only_end = read_only_free =
1403         (lispobj *)SymbolValue(READ_ONLY_SPACE_FREE_POINTER);
1404     static_end = static_free =
1405         (lispobj *)SymbolValue(STATIC_SPACE_FREE_POINTER);
1406
1407 #ifdef PRINTNOISE
1408     printf(" roots");
1409     fflush(stdout);
1410 #endif
1411
1412 #ifdef GENCGC
1413     gc_assert((lispobj *)CONTROL_STACK_END > ((&read_only_roots)+1));
1414     setup_i386_stack_scav(((&static_roots)-2), (lispobj *)CONTROL_STACK_END);
1415 #endif
1416
1417     pscav(&static_roots, 1, 0);
1418     pscav(&read_only_roots, 1, 1);
1419
1420 #ifdef PRINTNOISE
1421     printf(" handlers");
1422     fflush(stdout);
1423 #endif
1424     pscav((lispobj *) interrupt_handlers,
1425           sizeof(interrupt_handlers) / sizeof(lispobj),
1426           0);
1427
1428 #ifdef PRINTNOISE
1429     printf(" stack");
1430     fflush(stdout);
1431 #endif
1432 #ifndef __i386__
1433     pscav((lispobj *)control_stack,
1434           current_control_stack_pointer - (lispobj *)CONTROL_STACK_START,
1435           0);
1436 #else
1437 #ifdef GENCGC
1438     pscav_i386_stack();
1439 #endif
1440 #ifdef WANT_CGC
1441     gc_assert((lispobj *)control_stack_end > ((&read_only_roots)+1));
1442     carefully_pscav_stack(((&read_only_roots)+1),
1443                           (lispobj *)CONTROL_STACK_END);
1444 #endif
1445 #endif
1446
1447 #ifdef PRINTNOISE
1448     printf(" bindings");
1449     fflush(stdout);
1450 #endif
1451 #if !defined(ibmrt) && !defined(__i386__)
1452     pscav( (lispobj *)BINDING_STACK_START,
1453           (lispobj *)current_binding_stack_pointer - (lispobj *)BINDING_STACK_START,
1454           0);
1455 #else
1456     pscav( (lispobj *)BINDING_STACK_START,
1457           (lispobj *)SymbolValue(BINDING_STACK_POINTER) -
1458           (lispobj *)BINDING_STACK_START,
1459           0);
1460 #endif
1461
1462 #ifdef SCAVENGE_READ_ONLY_SPACE
1463     if (SymbolValue(SCAVENGE_READ_ONLY_SPACE) != type_UnboundMarker
1464         && SymbolValue(SCAVENGE_READ_ONLY_SPACE) != NIL) {
1465       unsigned  read_only_space_size =
1466           (lispobj *)SymbolValue(READ_ONLY_SPACE_FREE_POINTER) -
1467           (lispobj *)READ_ONLY_SPACE_START;
1468       fprintf(stderr,
1469               "scavenging read only space: %d bytes\n",
1470               read_only_space_size * sizeof(lispobj));
1471       pscav( (lispobj *)READ_ONLY_SPACE_START, read_only_space_size, 0);
1472     }
1473 #endif
1474
1475 #ifdef PRINTNOISE
1476     printf(" static");
1477     fflush(stdout);
1478 #endif
1479     clean = (lispobj *)STATIC_SPACE_START;
1480     do {
1481         while (clean != static_free)
1482             clean = pscav(clean, static_free - clean, 0);
1483         laters = later_blocks;
1484         count = later_count;
1485         later_blocks = NULL;
1486         later_count = 0;
1487         while (laters != NULL) {
1488             for (i = 0; i < count; i++) {
1489                 if (laters->u[i].count == 0) {
1490                     ;
1491                 } else if (laters->u[i].count <= LATERMAXCOUNT) {
1492                     pscav(laters->u[i+1].ptr, laters->u[i].count, 1);
1493                     i++;
1494                 } else {
1495                     pscav(laters->u[i].ptr, 1, 1);
1496                 }
1497             }
1498             next = laters->next;
1499             free(laters);
1500             laters = next;
1501             count = LATERBLOCKSIZE;
1502         }
1503     } while (clean != static_free || later_blocks != NULL);
1504
1505 #ifdef PRINTNOISE
1506     printf(" cleanup");
1507     fflush(stdout);
1508 #endif
1509
1510 #if defined(WANT_CGC) && defined(X86_CGC_ACTIVE_P)
1511     if(SymbolValue(X86_CGC_ACTIVE_P) != T) {
1512         os_zero((os_vm_address_t) DYNAMIC_SPACE_START,
1513                 (os_vm_size_t) DYNAMIC_SPACE_SIZE);
1514     }
1515 #else
1516     os_zero((os_vm_address_t) DYNAMIC_SPACE_START,
1517             (os_vm_size_t) DYNAMIC_SPACE_SIZE);
1518 #endif
1519
1520     /* Zero the stack. Note that the stack is also zeroed by SUB-GC
1521      * calling SCRUB-CONTROL-STACK - this zeros the stack on the x86. */
1522 #ifndef __i386__
1523     os_zero((os_vm_address_t) current_control_stack_pointer,
1524             (os_vm_size_t) (CONTROL_STACK_SIZE -
1525                             ((current_control_stack_pointer -
1526                               (lispobj *)CONTROL_STACK_START) *
1527                              sizeof(lispobj))));
1528 #endif
1529
1530 #if defined(WANT_CGC) && defined(STATIC_BLUE_BAG)
1531     {
1532       lispobj bag = SymbolValue(STATIC_BLUE_BAG);
1533       struct cons*cons = (struct cons*)static_free;
1534       struct cons*pair = cons + 1;
1535       static_free += 2*WORDS_PER_CONS;
1536       if(bag == type_UnboundMarker)
1537         bag = NIL;
1538       cons->cdr = bag;
1539       cons->car = (lispobj)pair | type_ListPointer;
1540       pair->car = (lispobj)static_end;
1541       pair->cdr = (lispobj)static_free;
1542       bag = (lispobj)cons | type_ListPointer;
1543       SetSymbolValue(STATIC_BLUE_BAG, bag);
1544     }
1545 #endif
1546
1547     /* It helps to update the heap free pointers so that free_heap can
1548      * verify after it's done. */
1549     SetSymbolValue(READ_ONLY_SPACE_FREE_POINTER, (lispobj)read_only_free);
1550     SetSymbolValue(STATIC_SPACE_FREE_POINTER, (lispobj)static_free);
1551
1552 #if !defined(ibmrt) && !defined(__i386__)
1553     dynamic_space_free_pointer = DYNAMIC_SPACE_START;
1554 #else
1555 #if defined(WANT_CGC) && defined(X86_CGC_ACTIVE_P)
1556     /* X86 using CGC */
1557     if(SymbolValue(X86_CGC_ACTIVE_P) != T)
1558         SetSymbolValue(ALLOCATION_POINTER, (lispobj)DYNAMIC_SPACE_START);
1559     else
1560         cgc_free_heap();
1561 #else
1562 #if defined GENCGC
1563     gc_free_heap();
1564 #else
1565     /* ibmrt using GC */
1566     SetSymbolValue(ALLOCATION_POINTER, (lispobj)DYNAMIC_SPACE_START);
1567 #endif
1568 #endif
1569 #endif
1570
1571 #ifdef PRINTNOISE
1572     printf(" done]\n");
1573     fflush(stdout);
1574 #endif
1575
1576     return 0;
1577 }