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