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