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