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