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