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