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