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