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