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