ba07c215c6d06def1eb939525a923b55019780c9
[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!\n");
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!\n");
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     struct layout *layout =
541       (struct layout *) native_pointer(((struct instance *)native_pointer(thing))->slots[0]);
542     lispobj pure = layout->pure;
543
544     switch (pure) {
545     case T:
546         return (ptrans_boxed(thing, header, 1));
547     case NIL:
548         return (ptrans_boxed(thing, header, 0));
549     case 0:
550         {
551             /* Substructure: special case for the COMPACT-INFO-ENVs,
552              * where the instance may have a point to the dynamic
553              * space placed into it (e.g. the cache-name slot), but
554              * the lists and arrays at the time of a purify can be
555              * moved to the RO space. */
556             long nwords;
557             lispobj result, *new, *old;
558
559             nwords = CEILING(1 + HeaderValue(header), 2);
560
561             /* Allocate it */
562             old = (lispobj *)native_pointer(thing);
563             new = newspace_alloc(nwords, 0); /*  inconstant */
564
565             /* Copy it. */
566             bcopy(old, new, nwords * sizeof(lispobj));
567
568             /* Deposit forwarding pointer. */
569             result = make_lispobj(new, lowtag_of(thing));
570             *old = result;
571
572             /* Scavenge it. */
573             pscav(new, nwords, 1);
574
575             return result;
576         }
577     default:
578         gc_abort();
579         return NIL; /* dummy value: return something ... */
580     }
581 }
582
583 static lispobj
584 ptrans_fdefn(lispobj thing, lispobj header)
585 {
586     long nwords;
587     lispobj result, *new, *old, oldfn;
588     struct fdefn *fdefn;
589
590     nwords = CEILING(1 + HeaderValue(header), 2);
591
592     /* Allocate it */
593     old = (lispobj *)native_pointer(thing);
594     new = newspace_alloc(nwords, 0);    /* inconstant */
595
596     /* Copy it. */
597     bcopy(old, new, nwords * sizeof(lispobj));
598
599     /* Deposit forwarding pointer. */
600     result = make_lispobj(new, lowtag_of(thing));
601     *old = result;
602
603     /* Scavenge the function. */
604     fdefn = (struct fdefn *)new;
605     oldfn = fdefn->fun;
606     pscav(&fdefn->fun, 1, 0);
607     if ((char *)oldfn + FUN_RAW_ADDR_OFFSET == fdefn->raw_addr)
608         fdefn->raw_addr = (char *)fdefn->fun + FUN_RAW_ADDR_OFFSET;
609
610     return result;
611 }
612
613 static lispobj
614 ptrans_unboxed(lispobj thing, lispobj header)
615 {
616     long nwords;
617     lispobj result, *new, *old;
618
619     nwords = CEILING(1 + HeaderValue(header), 2);
620
621     /* Allocate it */
622     old = (lispobj *)native_pointer(thing);
623     new = newspace_alloc(nwords,1);     /* always constant */
624
625     /* copy it. */
626     bcopy(old, new, nwords * sizeof(lispobj));
627
628     /* Deposit forwarding pointer. */
629     result = make_lispobj(new , lowtag_of(thing));
630     *old = result;
631
632     return result;
633 }
634
635 static lispobj
636 ptrans_vector(lispobj thing, long bits, long extra,
637               boolean boxed, boolean constant)
638 {
639     struct vector *vector;
640     long nwords;
641     lispobj result, *new;
642     long length;
643
644     vector = (struct vector *)native_pointer(thing);
645     length = fixnum_value(vector->length)+extra;
646     // Argh, handle simple-vector-nil separately.
647     if (bits == 0) {
648       nwords = 2;
649     } else {
650       nwords = CEILING(NWORDS(length, bits) + 2, 2);
651     }
652
653     new=newspace_alloc(nwords, (constant || !boxed));
654     bcopy(vector, new, nwords * sizeof(lispobj));
655
656     result = make_lispobj(new, lowtag_of(thing));
657     vector->header = result;
658
659     if (boxed)
660         pscav(new, nwords, constant);
661
662     return result;
663 }
664
665 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
666 static void
667 apply_code_fixups_during_purify(struct code *old_code, struct code *new_code)
668 {
669     long nheader_words, ncode_words, nwords;
670     void  *constants_start_addr, *constants_end_addr;
671     void  *code_start_addr, *code_end_addr;
672     lispobj fixups = NIL;
673     unsigned  displacement = (unsigned)new_code - (unsigned)old_code;
674     struct vector *fixups_vector;
675
676     ncode_words = fixnum_value(new_code->code_size);
677     nheader_words = HeaderValue(*(lispobj *)new_code);
678     nwords = ncode_words + nheader_words;
679
680     constants_start_addr = (void *)new_code + 5 * N_WORD_BYTES;
681     constants_end_addr = (void *)new_code + nheader_words*N_WORD_BYTES;
682     code_start_addr = (void *)new_code + nheader_words*N_WORD_BYTES;
683     code_end_addr = (void *)new_code + nwords*N_WORD_BYTES;
684
685     /* The first constant should be a pointer to the fixups for this
686      * code objects. Check. */
687     fixups = new_code->constants[0];
688
689     /* It will be 0 or the unbound-marker if there are no fixups, and
690      * will be an other-pointer to a vector if it is valid. */
691     if ((fixups==0) ||
692         (fixups==UNBOUND_MARKER_WIDETAG) ||
693         !is_lisp_pointer(fixups)) {
694 #ifdef LISP_FEATURE_GENCGC
695         /* Check for a possible errors. */
696         sniff_code_object(new_code,displacement);
697 #endif
698         return;
699     }
700
701     fixups_vector = (struct vector *)native_pointer(fixups);
702
703     /* Could be pointing to a forwarding pointer. */
704     if (is_lisp_pointer(fixups) && (dynamic_pointer_p(fixups))
705         && forwarding_pointer_p(*(lispobj *)fixups_vector)) {
706         /* If so then follow it. */
707         fixups_vector =
708             (struct vector *)native_pointer(*(lispobj *)fixups_vector);
709     }
710
711     if (widetag_of(fixups_vector->header) == SIMPLE_ARRAY_WORD_WIDETAG) {
712         /* We got the fixups for the code block. Now work through the
713          * vector, and apply a fixup at each address. */
714         long length = fixnum_value(fixups_vector->length);
715         long i;
716         for (i=0; i<length; i++) {
717             unsigned offset = fixups_vector->data[i];
718             /* Now check the current value of offset. */
719             unsigned old_value =
720                 *(unsigned *)((unsigned)code_start_addr + offset);
721
722             /* If it's within the old_code object then it must be an
723              * absolute fixup (relative ones are not saved) */
724             if ((old_value>=(unsigned)old_code)
725                 && (old_value<((unsigned)old_code + nwords * N_WORD_BYTES)))
726                 /* So add the dispacement. */
727                 *(unsigned *)((unsigned)code_start_addr + offset) = old_value
728                     + displacement;
729             else
730                 /* It is outside the old code object so it must be a relative
731                  * fixup (absolute fixups are not saved). So subtract the
732                  * displacement. */
733                 *(unsigned *)((unsigned)code_start_addr + offset) = old_value
734                     - displacement;
735         }
736     }
737
738     /* No longer need the fixups. */
739     new_code->constants[0] = 0;
740
741 #ifdef LISP_FEATURE_GENCGC
742     /* Check for possible errors. */
743     sniff_code_object(new_code,displacement);
744 #endif
745 }
746 #endif
747
748 static lispobj
749 ptrans_code(lispobj thing)
750 {
751     struct code *code, *new;
752     long nwords;
753     lispobj func, result;
754
755     code = (struct code *)native_pointer(thing);
756     nwords = CEILING(HeaderValue(code->header) + fixnum_value(code->code_size),
757                      2);
758
759     new = (struct code *)newspace_alloc(nwords,1); /* constant */
760
761     bcopy(code, new, nwords * sizeof(lispobj));
762
763 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
764     apply_code_fixups_during_purify(code,new);
765 #endif
766
767     result = make_lispobj(new, OTHER_POINTER_LOWTAG);
768
769     /* Stick in a forwarding pointer for the code object. */
770     *(lispobj *)code = result;
771
772     /* Put in forwarding pointers for all the functions. */
773     for (func = code->entry_points;
774          func != NIL;
775          func = ((struct simple_fun *)native_pointer(func))->next) {
776
777         gc_assert(lowtag_of(func) == FUN_POINTER_LOWTAG);
778
779         *(lispobj *)native_pointer(func) = result + (func - thing);
780     }
781
782     /* Arrange to scavenge the debug info later. */
783     pscav_later(&new->debug_info, 1);
784
785     /* FIXME: why would this be a fixnum? */
786     /* "why" is a hard word, but apparently for compiled functions the
787        trace_table_offset contains the length of the instructions, as
788        a fixnum.  See CODE-INST-AREA-LENGTH in
789        src/compiler/target-disassem.lisp.  -- CSR, 2004-01-08 */
790     if (!(fixnump(new->trace_table_offset)))
791 #if 0
792         pscav(&new->trace_table_offset, 1, 0);
793 #else
794         new->trace_table_offset = NIL; /* limit lifetime */
795 #endif
796
797     /* Scavenge the constants. */
798     pscav(new->constants, HeaderValue(new->header)-5, 1);
799
800     /* Scavenge all the functions. */
801     pscav(&new->entry_points, 1, 1);
802     for (func = new->entry_points;
803          func != NIL;
804          func = ((struct simple_fun *)native_pointer(func))->next) {
805         gc_assert(lowtag_of(func) == FUN_POINTER_LOWTAG);
806         gc_assert(!dynamic_pointer_p(func));
807
808 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
809         /* Temporarily convert the self pointer to a real function pointer. */
810         ((struct simple_fun *)native_pointer(func))->self
811             -= FUN_RAW_ADDR_OFFSET;
812 #endif
813         pscav(&((struct simple_fun *)native_pointer(func))->self, 2, 1);
814 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
815         ((struct simple_fun *)native_pointer(func))->self
816             += FUN_RAW_ADDR_OFFSET;
817 #endif
818         pscav_later(&((struct simple_fun *)native_pointer(func))->name, 3);
819     }
820
821     return result;
822 }
823
824 static lispobj
825 ptrans_func(lispobj thing, lispobj header)
826 {
827     long nwords;
828     lispobj code, *new, *old, result;
829     struct simple_fun *function;
830
831     /* Thing can either be a function header, a closure function
832      * header, a closure, or a funcallable-instance. If it's a closure
833      * or a funcallable-instance, we do the same as ptrans_boxed.
834      * Otherwise we have to do something strange, 'cause it is buried
835      * inside a code object. */
836
837     if (widetag_of(header) == SIMPLE_FUN_HEADER_WIDETAG) {
838
839         /* We can only end up here if the code object has not been
840          * scavenged, because if it had been scavenged, forwarding pointers
841          * would have been left behind for all the entry points. */
842
843         function = (struct simple_fun *)native_pointer(thing);
844         code =
845             make_lispobj
846             ((native_pointer(thing) -
847               (HeaderValue(function->header))), OTHER_POINTER_LOWTAG);
848
849         /* This will cause the function's header to be replaced with a
850          * forwarding pointer. */
851
852         ptrans_code(code);
853
854         /* So we can just return that. */
855         return function->header;
856     }
857     else {
858         /* It's some kind of closure-like thing. */
859         nwords = CEILING(1 + HeaderValue(header), 2);
860         old = (lispobj *)native_pointer(thing);
861
862         /* Allocate the new one.  FINs *must* not go in read_only
863          * space.  Closures can; they never change */
864
865         new = newspace_alloc
866             (nwords,(widetag_of(header)!=FUNCALLABLE_INSTANCE_HEADER_WIDETAG));
867
868         /* Copy it. */
869         bcopy(old, new, nwords * sizeof(lispobj));
870
871         /* Deposit forwarding pointer. */
872         result = make_lispobj(new, lowtag_of(thing));
873         *old = result;
874
875         /* Scavenge it. */
876         pscav(new, nwords, 0);
877
878         return result;
879     }
880 }
881
882 static lispobj
883 ptrans_returnpc(lispobj thing, lispobj header)
884 {
885     lispobj code, new;
886
887     /* Find the corresponding code object. */
888     code = thing - HeaderValue(header)*sizeof(lispobj);
889
890     /* Make sure it's been transported. */
891     new = *(lispobj *)native_pointer(code);
892     if (!forwarding_pointer_p(new))
893         new = ptrans_code(code);
894
895     /* Maintain the offset: */
896     return new + (thing - code);
897 }
898
899 #define WORDS_PER_CONS CEILING(sizeof(struct cons) / sizeof(lispobj), 2)
900
901 static lispobj
902 ptrans_list(lispobj thing, boolean constant)
903 {
904     struct cons *old, *new, *orig;
905     long length;
906
907     orig = (struct cons *) newspace_alloc(0,constant);
908     length = 0;
909
910     do {
911         /* Allocate a new cons cell. */
912         old = (struct cons *)native_pointer(thing);
913         new = (struct cons *) newspace_alloc(WORDS_PER_CONS,constant);
914
915         /* Copy the cons cell and keep a pointer to the cdr. */
916         new->car = old->car;
917         thing = new->cdr = old->cdr;
918
919         /* Set up the forwarding pointer. */
920         *(lispobj *)old = make_lispobj(new, LIST_POINTER_LOWTAG);
921
922         /* And count this cell. */
923         length++;
924     } while (lowtag_of(thing) == LIST_POINTER_LOWTAG &&
925              dynamic_pointer_p(thing) &&
926              !(forwarding_pointer_p(*(lispobj *)native_pointer(thing))));
927
928     /* Scavenge the list we just copied. */
929     pscav((lispobj *)orig, length * WORDS_PER_CONS, constant);
930
931     return make_lispobj(orig, LIST_POINTER_LOWTAG);
932 }
933
934 static lispobj
935 ptrans_otherptr(lispobj thing, lispobj header, boolean constant)
936 {
937     switch (widetag_of(header)) {
938         /* FIXME: this needs a reindent */
939       case BIGNUM_WIDETAG:
940       case SINGLE_FLOAT_WIDETAG:
941       case DOUBLE_FLOAT_WIDETAG:
942 #ifdef LONG_FLOAT_WIDETAG
943       case LONG_FLOAT_WIDETAG:
944 #endif
945 #ifdef COMPLEX_SINGLE_FLOAT_WIDETAG
946       case COMPLEX_SINGLE_FLOAT_WIDETAG:
947 #endif
948 #ifdef COMPLEX_DOUBLE_FLOAT_WIDETAG
949       case COMPLEX_DOUBLE_FLOAT_WIDETAG:
950 #endif
951 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
952       case COMPLEX_LONG_FLOAT_WIDETAG:
953 #endif
954       case SAP_WIDETAG:
955           return ptrans_unboxed(thing, header);
956
957       case RATIO_WIDETAG:
958       case COMPLEX_WIDETAG:
959       case SIMPLE_ARRAY_WIDETAG:
960       case COMPLEX_BASE_STRING_WIDETAG:
961 #ifdef COMPLEX_CHARACTER_STRING_WIDETAG
962     case COMPLEX_CHARACTER_STRING_WIDETAG:
963 #endif
964       case COMPLEX_BIT_VECTOR_WIDETAG:
965       case COMPLEX_VECTOR_NIL_WIDETAG:
966       case COMPLEX_VECTOR_WIDETAG:
967       case COMPLEX_ARRAY_WIDETAG:
968         return ptrans_boxed(thing, header, constant);
969
970       case VALUE_CELL_HEADER_WIDETAG:
971       case WEAK_POINTER_WIDETAG:
972         return ptrans_boxed(thing, header, 0);
973
974       case SYMBOL_HEADER_WIDETAG:
975         return ptrans_boxed(thing, header, 0);
976
977       case SIMPLE_ARRAY_NIL_WIDETAG:
978         return ptrans_vector(thing, 0, 0, 0, constant);
979
980       case SIMPLE_BASE_STRING_WIDETAG:
981         return ptrans_vector(thing, 8, 1, 0, constant);
982
983 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
984     case SIMPLE_CHARACTER_STRING_WIDETAG:
985         return ptrans_vector(thing, 32, 1, 0, constant);
986 #endif
987
988       case SIMPLE_BIT_VECTOR_WIDETAG:
989         return ptrans_vector(thing, 1, 0, 0, constant);
990
991       case SIMPLE_VECTOR_WIDETAG:
992         return ptrans_vector(thing, N_WORD_BITS, 0, 1, constant);
993
994       case SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG:
995         return ptrans_vector(thing, 2, 0, 0, constant);
996
997       case SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG:
998         return ptrans_vector(thing, 4, 0, 0, constant);
999
1000       case SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG:
1001 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
1002       case SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG:
1003       case SIMPLE_ARRAY_UNSIGNED_BYTE_7_WIDETAG:
1004 #endif
1005         return ptrans_vector(thing, 8, 0, 0, constant);
1006
1007       case SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG:
1008 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
1009       case SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG:
1010       case SIMPLE_ARRAY_UNSIGNED_BYTE_15_WIDETAG:
1011 #endif
1012         return ptrans_vector(thing, 16, 0, 0, constant);
1013
1014       case SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG:
1015 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG
1016       case SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG:
1017       case SIMPLE_ARRAY_UNSIGNED_BYTE_29_WIDETAG:
1018 #endif
1019 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
1020       case SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG:
1021       case SIMPLE_ARRAY_UNSIGNED_BYTE_31_WIDETAG:
1022 #endif
1023         return ptrans_vector(thing, 32, 0, 0, constant);
1024
1025 #if N_WORD_BITS == 64
1026 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_60_WIDETAG
1027       case SIMPLE_ARRAY_UNSIGNED_BYTE_60_WIDETAG:
1028 #endif
1029 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG
1030       case SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG:
1031 #endif
1032 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG
1033       case SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG:
1034 #endif
1035 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_61_WIDETAG
1036       case SIMPLE_ARRAY_SIGNED_BYTE_61_WIDETAG:
1037 #endif
1038 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG
1039       case SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG:
1040 #endif
1041         return ptrans_vector(thing, 64, 0, 0, constant);
1042 #endif
1043
1044       case SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG:
1045         return ptrans_vector(thing, 32, 0, 0, constant);
1046
1047       case SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG:
1048         return ptrans_vector(thing, 64, 0, 0, constant);
1049
1050 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
1051       case SIMPLE_ARRAY_LONG_FLOAT_WIDETAG:
1052 #ifdef LISP_FEATURE_X86
1053         return ptrans_vector(thing, 96, 0, 0, constant);
1054 #endif
1055 #ifdef LISP_FEATURE_SPARC
1056         return ptrans_vector(thing, 128, 0, 0, constant);
1057 #endif
1058 #endif
1059
1060 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
1061       case SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG:
1062         return ptrans_vector(thing, 64, 0, 0, constant);
1063 #endif
1064
1065 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
1066       case SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG:
1067         return ptrans_vector(thing, 128, 0, 0, constant);
1068 #endif
1069
1070 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
1071       case SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG:
1072 #ifdef LISP_FEATURE_X86
1073         return ptrans_vector(thing, 192, 0, 0, constant);
1074 #endif
1075 #ifdef LISP_FEATURE_SPARC
1076         return ptrans_vector(thing, 256, 0, 0, constant);
1077 #endif
1078 #endif
1079
1080       case CODE_HEADER_WIDETAG:
1081         return ptrans_code(thing);
1082
1083       case RETURN_PC_HEADER_WIDETAG:
1084         return ptrans_returnpc(thing, header);
1085
1086       case FDEFN_WIDETAG:
1087         return ptrans_fdefn(thing, header);
1088
1089       default:
1090         fprintf(stderr, "Invalid widetag: %d\n", widetag_of(header));
1091         /* Should only come across other pointers to the above stuff. */
1092         gc_abort();
1093         return NIL;
1094     }
1095 }
1096
1097 static long
1098 pscav_fdefn(struct fdefn *fdefn)
1099 {
1100     boolean fix_func;
1101
1102     fix_func = ((char *)(fdefn->fun+FUN_RAW_ADDR_OFFSET) == fdefn->raw_addr);
1103     pscav(&fdefn->name, 1, 1);
1104     pscav(&fdefn->fun, 1, 0);
1105     if (fix_func)
1106         fdefn->raw_addr = (char *)(fdefn->fun + FUN_RAW_ADDR_OFFSET);
1107     return sizeof(struct fdefn) / sizeof(lispobj);
1108 }
1109
1110 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
1111 /* now putting code objects in static space */
1112 static long
1113 pscav_code(struct code*code)
1114 {
1115     long nwords;
1116     lispobj func;
1117     nwords = CEILING(HeaderValue(code->header) + fixnum_value(code->code_size),
1118                      2);
1119
1120     /* Arrange to scavenge the debug info later. */
1121     pscav_later(&code->debug_info, 1);
1122
1123     /* Scavenge the constants. */
1124     pscav(code->constants, HeaderValue(code->header)-5, 1);
1125
1126     /* Scavenge all the functions. */
1127     pscav(&code->entry_points, 1, 1);
1128     for (func = code->entry_points;
1129          func != NIL;
1130          func = ((struct simple_fun *)native_pointer(func))->next) {
1131         gc_assert(lowtag_of(func) == FUN_POINTER_LOWTAG);
1132         gc_assert(!dynamic_pointer_p(func));
1133
1134 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
1135         /* Temporarily convert the self pointer to a real function
1136          * pointer. */
1137         ((struct simple_fun *)native_pointer(func))->self
1138             -= FUN_RAW_ADDR_OFFSET;
1139 #endif
1140         pscav(&((struct simple_fun *)native_pointer(func))->self, 2, 1);
1141 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
1142         ((struct simple_fun *)native_pointer(func))->self
1143             += FUN_RAW_ADDR_OFFSET;
1144 #endif
1145         pscav_later(&((struct simple_fun *)native_pointer(func))->name, 3);
1146     }
1147
1148     return CEILING(nwords,2);
1149 }
1150 #endif
1151
1152 static lispobj *
1153 pscav(lispobj *addr, long nwords, boolean constant)
1154 {
1155     lispobj thing, *thingp, header;
1156     long count = 0; /* (0 = dummy init value to stop GCC warning) */
1157     struct vector *vector;
1158
1159     while (nwords > 0) {
1160         thing = *addr;
1161         if (is_lisp_pointer(thing)) {
1162             /* It's a pointer. Is it something we might have to move? */
1163             if (dynamic_pointer_p(thing)) {
1164                 /* Maybe. Have we already moved it? */
1165                 thingp = (lispobj *)native_pointer(thing);
1166                 header = *thingp;
1167                 if (is_lisp_pointer(header) && forwarding_pointer_p(header))
1168                     /* Yep, so just copy the forwarding pointer. */
1169                     thing = header;
1170                 else {
1171                     /* Nope, copy the object. */
1172                     switch (lowtag_of(thing)) {
1173                       case FUN_POINTER_LOWTAG:
1174                         thing = ptrans_func(thing, header);
1175                         break;
1176
1177                       case LIST_POINTER_LOWTAG:
1178                         thing = ptrans_list(thing, constant);
1179                         break;
1180
1181                       case INSTANCE_POINTER_LOWTAG:
1182                         thing = ptrans_instance(thing, header, constant);
1183                         break;
1184
1185                       case OTHER_POINTER_LOWTAG:
1186                         thing = ptrans_otherptr(thing, header, constant);
1187                         break;
1188
1189                       default:
1190                         /* It was a pointer, but not one of them? */
1191                         gc_abort();
1192                     }
1193                 }
1194                 *addr = thing;
1195             }
1196             count = 1;
1197         }
1198 #if N_WORD_BITS == 64
1199         else if (widetag_of(thing) == SINGLE_FLOAT_WIDETAG) {
1200             count = 1;
1201         }
1202 #endif
1203         else if (thing & FIXNUM_TAG_MASK) {
1204             /* It's an other immediate. Maybe the header for an unboxed */
1205             /* object. */
1206             switch (widetag_of(thing)) {
1207               case BIGNUM_WIDETAG:
1208               case SINGLE_FLOAT_WIDETAG:
1209               case DOUBLE_FLOAT_WIDETAG:
1210 #ifdef LONG_FLOAT_WIDETAG
1211               case LONG_FLOAT_WIDETAG:
1212 #endif
1213               case SAP_WIDETAG:
1214                 /* It's an unboxed simple object. */
1215                 count = CEILING(HeaderValue(thing)+1, 2);
1216                 break;
1217
1218               case SIMPLE_VECTOR_WIDETAG:
1219                   if (HeaderValue(thing) == subtype_VectorValidHashing) {
1220                     *addr = (subtype_VectorMustRehash << N_WIDETAG_BITS) |
1221                         SIMPLE_VECTOR_WIDETAG;
1222                   }
1223                 count = 2;
1224                 break;
1225
1226               case SIMPLE_ARRAY_NIL_WIDETAG:
1227                 count = 2;
1228                 break;
1229
1230               case SIMPLE_BASE_STRING_WIDETAG:
1231                 vector = (struct vector *)addr;
1232                 count = CEILING(NWORDS(fixnum_value(vector->length)+1,8)+2,2);
1233                 break;
1234
1235 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
1236             case SIMPLE_CHARACTER_STRING_WIDETAG:
1237                 vector = (struct vector *)addr;
1238                 count = CEILING(NWORDS(fixnum_value(vector->length)+1,32)+2,2);
1239                 break;
1240 #endif
1241
1242               case SIMPLE_BIT_VECTOR_WIDETAG:
1243                 vector = (struct vector *)addr;
1244                 count = CEILING(NWORDS(fixnum_value(vector->length),1)+2,2);
1245                 break;
1246
1247               case SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG:
1248                 vector = (struct vector *)addr;
1249                 count = CEILING(NWORDS(fixnum_value(vector->length),2)+2,2);
1250                 break;
1251
1252               case SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG:
1253                 vector = (struct vector *)addr;
1254                 count = CEILING(NWORDS(fixnum_value(vector->length),4)+2,2);
1255                 break;
1256
1257               case SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG:
1258 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
1259               case SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG:
1260               case SIMPLE_ARRAY_UNSIGNED_BYTE_7_WIDETAG:
1261 #endif
1262                 vector = (struct vector *)addr;
1263                 count = CEILING(NWORDS(fixnum_value(vector->length),8)+2,2);
1264                 break;
1265
1266               case SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG:
1267 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
1268               case SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG:
1269               case SIMPLE_ARRAY_UNSIGNED_BYTE_15_WIDETAG:
1270 #endif
1271                 vector = (struct vector *)addr;
1272                 count = CEILING(NWORDS(fixnum_value(vector->length),16)+2,2);
1273                 break;
1274
1275               case SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG:
1276 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG
1277               case SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG:
1278               case SIMPLE_ARRAY_UNSIGNED_BYTE_29_WIDETAG:
1279 #endif
1280 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
1281               case SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG:
1282               case SIMPLE_ARRAY_UNSIGNED_BYTE_31_WIDETAG:
1283 #endif
1284                 vector = (struct vector *)addr;
1285                 count = CEILING(NWORDS(fixnum_value(vector->length),32)+2,2);
1286                 break;
1287
1288 #if N_WORD_BITS == 64
1289               case SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG:
1290 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_61_WIDETAG
1291               case SIMPLE_ARRAY_SIGNED_BYTE_61_WIDETAG:
1292               case SIMPLE_ARRAY_UNSIGNED_BYTE_60_WIDETAG:
1293 #endif
1294 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG
1295               case SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG:
1296               case SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG:
1297 #endif
1298                 vector = (struct vector *)addr;
1299                 count = CEILING(NWORDS(fixnum_value(vector->length),64)+2,2);
1300                 break;
1301 #endif
1302
1303               case SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG:
1304                 vector = (struct vector *)addr;
1305                 count = CEILING(NWORDS(fixnum_value(vector->length), 32) + 2,
1306                                 2);
1307                 break;
1308
1309               case SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG:
1310 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
1311               case SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG:
1312 #endif
1313                 vector = (struct vector *)addr;
1314                 count = CEILING(NWORDS(fixnum_value(vector->length), 64) + 2,
1315                                 2);
1316                 break;
1317
1318 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
1319               case SIMPLE_ARRAY_LONG_FLOAT_WIDETAG:
1320                 vector = (struct vector *)addr;
1321 #ifdef LISP_FEATURE_X86
1322                 count = fixnum_value(vector->length)*3+2;
1323 #endif
1324 #ifdef LISP_FEATURE_SPARC
1325                 count = fixnum_value(vector->length)*4+2;
1326 #endif
1327                 break;
1328 #endif
1329
1330 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
1331               case SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG:
1332                 vector = (struct vector *)addr;
1333                 count = CEILING(NWORDS(fixnum_value(vector->length), 128) + 2,
1334                                 2);
1335                 break;
1336 #endif
1337
1338 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
1339               case SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG:
1340                 vector = (struct vector *)addr;
1341 #ifdef LISP_FEATURE_X86
1342                 count = fixnum_value(vector->length)*6+2;
1343 #endif
1344 #ifdef LISP_FEATURE_SPARC
1345                 count = fixnum_value(vector->length)*8+2;
1346 #endif
1347                 break;
1348 #endif
1349
1350               case CODE_HEADER_WIDETAG:
1351 #if !defined(LISP_FEATURE_X86) && !defined(LISP_FEATURE_X86_64)
1352                 gc_abort(); /* no code headers in static space */
1353 #else
1354                 count = pscav_code((struct code*)addr);
1355 #endif
1356                 break;
1357
1358               case SIMPLE_FUN_HEADER_WIDETAG:
1359               case RETURN_PC_HEADER_WIDETAG:
1360                 /* We should never hit any of these, 'cause they occur
1361                  * buried in the middle of code objects. */
1362                 gc_abort();
1363                 break;
1364
1365 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
1366               case CLOSURE_HEADER_WIDETAG:
1367               case FUNCALLABLE_INSTANCE_HEADER_WIDETAG:
1368                 /* The function self pointer needs special care on the
1369                  * x86 because it is the real entry point. */
1370                 {
1371                   lispobj fun = ((struct closure *)addr)->fun
1372                     - FUN_RAW_ADDR_OFFSET;
1373                   pscav(&fun, 1, constant);
1374                   ((struct closure *)addr)->fun = fun + FUN_RAW_ADDR_OFFSET;
1375                 }
1376                 count = 2;
1377                 break;
1378 #endif
1379
1380               case WEAK_POINTER_WIDETAG:
1381                 /* Weak pointers get preserved during purify, 'cause I
1382                  * don't feel like figuring out how to break them. */
1383                 pscav(addr+1, 2, constant);
1384                 count = 4;
1385                 break;
1386
1387               case FDEFN_WIDETAG:
1388                 /* We have to handle fdefn objects specially, so we
1389                  * can fix up the raw function address. */
1390                 count = pscav_fdefn((struct fdefn *)addr);
1391                 break;
1392
1393               case INSTANCE_HEADER_WIDETAG:
1394                 {
1395                     struct instance *instance = (struct instance *) addr;
1396                     struct layout *layout
1397                         = (struct layout *) native_pointer(instance->slots[0]);
1398                     long nuntagged = fixnum_value(layout->n_untagged_slots);
1399                     long nslots = HeaderValue(*addr);
1400                     pscav(addr + 1, nslots - nuntagged, constant);
1401                     count = CEILING(1 + nslots, 2);
1402                 }
1403                 break;
1404
1405               default:
1406                 count = 1;
1407                 break;
1408             }
1409         }
1410         else {
1411             /* It's a fixnum. */
1412             count = 1;
1413         }
1414
1415         addr += count;
1416         nwords -= count;
1417     }
1418
1419     return addr;
1420 }
1421
1422 int
1423 purify(lispobj static_roots, lispobj read_only_roots)
1424 {
1425     lispobj *clean;
1426     long count, i;
1427     struct later *laters, *next;
1428     struct thread *thread;
1429
1430     if(all_threads->next) {
1431         /* FIXME: there should be _some_ sensible error reporting
1432          * convention.  See following comment too */
1433         fprintf(stderr,"Can't purify when more than one thread exists\n");
1434         fflush(stderr);
1435         return 0;
1436     }
1437
1438 #ifdef PRINTNOISE
1439     printf("[doing purification:");
1440     fflush(stdout);
1441 #endif
1442 #ifdef LISP_FEATURE_GENCGC
1443     gc_alloc_update_all_page_tables();
1444 #endif
1445     for_each_thread(thread)
1446         if (fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX,thread)) != 0) {
1447         /* FIXME: 1. What does this mean? 2. It shouldn't be reporting
1448          * its error simply by a. printing a string b. to stdout instead
1449          * of stderr. */
1450         printf(" Ack! Can't purify interrupt contexts. ");
1451         fflush(stdout);
1452         return 0;
1453     }
1454
1455 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
1456     dynamic_space_free_pointer =
1457       (lispobj*)SymbolValue(ALLOCATION_POINTER,0);
1458 #endif
1459
1460     read_only_end = read_only_free =
1461         (lispobj *)SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0);
1462     static_end = static_free =
1463         (lispobj *)SymbolValue(STATIC_SPACE_FREE_POINTER,0);
1464
1465 #ifdef PRINTNOISE
1466     printf(" roots");
1467     fflush(stdout);
1468 #endif
1469
1470 #if defined(LISP_FEATURE_GENCGC) && (defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64))
1471     /* note this expects only one thread to be active.  We'd have to
1472      * stop all the others in the same way as GC does if we wanted
1473      * PURIFY to work when >1 thread exists */
1474     setup_i386_stack_scav(((&static_roots)-2),
1475                           ((void *)all_threads->control_stack_end));
1476 #endif
1477
1478     pscav(&static_roots, 1, 0);
1479     pscav(&read_only_roots, 1, 1);
1480
1481 #ifdef PRINTNOISE
1482     printf(" handlers");
1483     fflush(stdout);
1484 #endif
1485     pscav((lispobj *) interrupt_handlers,
1486           sizeof(interrupt_handlers) / sizeof(lispobj),
1487           0);
1488
1489 #ifdef PRINTNOISE
1490     printf(" stack");
1491     fflush(stdout);
1492 #endif
1493 #if !defined(LISP_FEATURE_X86) && !defined(LISP_FEATURE_X86_64)
1494     pscav((lispobj *)all_threads->control_stack_start,
1495           current_control_stack_pointer -
1496           all_threads->control_stack_start,
1497           0);
1498 #else
1499 #ifdef LISP_FEATURE_GENCGC
1500     pscav_i386_stack();
1501 #endif
1502 #endif
1503
1504 #ifdef PRINTNOISE
1505     printf(" bindings");
1506     fflush(stdout);
1507 #endif
1508 #if !(defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64))
1509     pscav( (lispobj *)all_threads->binding_stack_start,
1510           (lispobj *)current_binding_stack_pointer -
1511            all_threads->binding_stack_start,
1512           0);
1513 #else
1514     for_each_thread(thread) {
1515         pscav( (lispobj *)thread->binding_stack_start,
1516                (lispobj *)SymbolValue(BINDING_STACK_POINTER,thread) -
1517                (lispobj *)thread->binding_stack_start,
1518           0);
1519         pscav( (lispobj *) (thread+1),
1520                fixnum_value(SymbolValue(FREE_TLS_INDEX,0)) -
1521                (sizeof (struct thread))/(sizeof (lispobj)),
1522           0);
1523     }
1524
1525
1526 #endif
1527
1528     /* The original CMU CL code had scavenge-read-only-space code
1529      * controlled by the Lisp-level variable
1530      * *SCAVENGE-READ-ONLY-SPACE*. It was disabled by default, and it
1531      * wasn't documented under what circumstances it was useful or
1532      * safe to turn it on, so it's been turned off in SBCL. If you
1533      * want/need this functionality, and can test and document it,
1534      * please submit a patch. */
1535 #if 0
1536     if (SymbolValue(SCAVENGE_READ_ONLY_SPACE) != UNBOUND_MARKER_WIDETAG
1537         && SymbolValue(SCAVENGE_READ_ONLY_SPACE) != NIL) {
1538       unsigned  read_only_space_size =
1539           (lispobj *)SymbolValue(READ_ONLY_SPACE_FREE_POINTER) -
1540           (lispobj *)READ_ONLY_SPACE_START;
1541       fprintf(stderr,
1542               "scavenging read only space: %d bytes\n",
1543               read_only_space_size * sizeof(lispobj));
1544       pscav( (lispobj *)READ_ONLY_SPACE_START, read_only_space_size, 0);
1545     }
1546 #endif
1547
1548 #ifdef PRINTNOISE
1549     printf(" static");
1550     fflush(stdout);
1551 #endif
1552     clean = (lispobj *)STATIC_SPACE_START;
1553     do {
1554         while (clean != static_free)
1555             clean = pscav(clean, static_free - clean, 0);
1556         laters = later_blocks;
1557         count = later_count;
1558         later_blocks = NULL;
1559         later_count = 0;
1560         while (laters != NULL) {
1561             for (i = 0; i < count; i++) {
1562                 if (laters->u[i].count == 0) {
1563                     ;
1564                 } else if (laters->u[i].count <= LATERMAXCOUNT) {
1565                     pscav(laters->u[i+1].ptr, laters->u[i].count, 1);
1566                     i++;
1567                 } else {
1568                     pscav(laters->u[i].ptr, 1, 1);
1569                 }
1570             }
1571             next = laters->next;
1572             free(laters);
1573             laters = next;
1574             count = LATERBLOCKSIZE;
1575         }
1576     } while (clean != static_free || later_blocks != NULL);
1577
1578 #ifdef PRINTNOISE
1579     printf(" cleanup");
1580     fflush(stdout);
1581 #endif
1582
1583     os_zero((os_vm_address_t) current_dynamic_space,
1584             (os_vm_size_t) DYNAMIC_SPACE_SIZE);
1585
1586     /* Zero the stack. Note that the stack is also zeroed by SUB-GC
1587      * calling SCRUB-CONTROL-STACK - this zeros the stack on the x86. */
1588 #if !defined(LISP_FEATURE_X86) && !defined(LISP_FEATURE_X86_64)
1589     os_zero((os_vm_address_t) current_control_stack_pointer,
1590             (os_vm_size_t)
1591             ((all_threads->control_stack_end -
1592               current_control_stack_pointer) * sizeof(lispobj)));
1593 #endif
1594
1595     /* It helps to update the heap free pointers so that free_heap can
1596      * verify after it's done. */
1597     SetSymbolValue(READ_ONLY_SPACE_FREE_POINTER, (lispobj)read_only_free,0);
1598     SetSymbolValue(STATIC_SPACE_FREE_POINTER, (lispobj)static_free,0);
1599
1600 #if !defined(ALLOCATION_POINTER)
1601     dynamic_space_free_pointer = current_dynamic_space;
1602     set_auto_gc_trigger(bytes_consed_between_gcs);
1603 #else
1604 #if defined LISP_FEATURE_GENCGC
1605     gc_free_heap();
1606 #else
1607 #error unsupported case /* in CMU CL, was "ibmrt using GC" */
1608 #endif
1609 #endif
1610
1611     /* Blast away instruction cache */
1612     os_flush_icache((os_vm_address_t)READ_ONLY_SPACE_START, READ_ONLY_SPACE_SIZE);
1613     os_flush_icache((os_vm_address_t)STATIC_SPACE_START, STATIC_SPACE_SIZE);
1614
1615 #ifdef PRINTNOISE
1616     printf(" done]\n");
1617     fflush(stdout);
1618 #endif
1619     return 0;
1620 }