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