0.9.18.29: --dynamic-space-size command-line switch
[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 extern unsigned long bytes_consed_between_gcs;
41
42 static lispobj *dynamic_space_purify_pointer;
43
44 \f
45 /* These hold the original end of the read_only and static spaces so
46  * we can tell what are forwarding pointers. */
47
48 static lispobj *read_only_end, *static_end;
49
50 static lispobj *read_only_free, *static_free;
51
52 static lispobj *pscav(lispobj *addr, long nwords, boolean constant);
53
54 #define LATERBLOCKSIZE 1020
55 #define LATERMAXCOUNT 10
56
57 static struct
58 later {
59     struct later *next;
60     union {
61         lispobj *ptr;
62         long count;
63     } u[LATERBLOCKSIZE];
64 } *later_blocks = NULL;
65 static long later_count = 0;
66
67 #if N_WORD_BITS == 32
68  #define SIMPLE_ARRAY_WORD_WIDETAG SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG
69 #elif N_WORD_BITS == 64
70  #define SIMPLE_ARRAY_WORD_WIDETAG SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG
71 #endif
72
73 /* FIXME: Shouldn't this be defined in sbcl.h?  See also notes in
74  * cheneygc.c */
75
76 #ifdef LISP_FEATURE_SPARC
77 #define FUN_RAW_ADDR_OFFSET 0
78 #else
79 #define FUN_RAW_ADDR_OFFSET (6*sizeof(lispobj) - FUN_POINTER_LOWTAG)
80 #endif
81 \f
82 static boolean
83 forwarding_pointer_p(lispobj obj)
84 {
85     lispobj *ptr = native_pointer(obj);
86
87     return ((static_end <= ptr && ptr <= static_free) ||
88             (read_only_end <= ptr && ptr <= read_only_free));
89 }
90
91 static boolean
92 dynamic_pointer_p(lispobj ptr)
93 {
94 #ifndef LISP_FEATURE_GENCGC
95     return (ptr >= (lispobj)current_dynamic_space
96             &&
97             ptr < (lispobj)dynamic_space_purify_pointer);
98 #else
99     /* Be more conservative, and remember, this is a maybe. */
100     return (ptr >= (lispobj)DYNAMIC_SPACE_START
101             &&
102             ptr < (lispobj)dynamic_space_purify_pointer);
103 #endif
104 }
105
106 static inline lispobj *
107 newspace_alloc(long nwords, int constantp)
108 {
109     lispobj *ret;
110     nwords=CEILING(nwords,2);
111     if(constantp) {
112         if(read_only_free + nwords >= (lispobj *)READ_ONLY_SPACE_END) {
113             lose("Ran out of read-only space while purifying!\n");
114         }
115         ret=read_only_free;
116         read_only_free+=nwords;
117     } else {
118         if(static_free + nwords >= (lispobj *)STATIC_SPACE_END) {
119             lose("Ran out of static space while purifying!\n");
120         }
121         ret=static_free;
122         static_free+=nwords;
123     }
124     return ret;
125 }
126
127
128 \f
129 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
130
131 #ifdef LISP_FEATURE_GENCGC
132 /*
133  * enhanced x86/GENCGC stack scavenging by Douglas Crosher
134  *
135  * Scavenging the stack on the i386 is problematic due to conservative
136  * roots and raw return addresses. Here it is handled in two passes:
137  * the first pass runs before any objects are moved and tries to
138  * identify valid pointers and return address on the stack, the second
139  * pass scavenges these.
140  */
141
142 static unsigned pointer_filter_verbose = 0;
143
144 /* FIXME: This is substantially the same code as
145  * possibly_valid_dynamic_space_pointer in gencgc.c.  The only
146  * relevant difference seems to be that the gencgc code also checks
147  * for raw pointers into Code objects, whereas in purify these are
148  * checked separately in setup_i386_stack_scav - they go onto
149  * valid_stack_ra_locations instead of just valid_stack_locations */
150
151 static int
152 valid_dynamic_space_pointer(lispobj *pointer, lispobj *start_addr)
153 {
154     /* If it's not a return address then it needs to be a valid Lisp
155      * pointer. */
156     if (!is_lisp_pointer((lispobj)pointer))
157         return 0;
158
159     /* Check that the object pointed to is consistent with the pointer
160      * low tag. */
161     switch (lowtag_of((lispobj)pointer)) {
162     case FUN_POINTER_LOWTAG:
163         /* Start_addr should be the enclosing code object, or a closure
164          * header. */
165         switch (widetag_of(*start_addr)) {
166         case CODE_HEADER_WIDETAG:
167             /* This case is probably caught above. */
168             break;
169         case CLOSURE_HEADER_WIDETAG:
170         case FUNCALLABLE_INSTANCE_HEADER_WIDETAG:
171             if ((long)pointer != ((long)start_addr+FUN_POINTER_LOWTAG)) {
172                 if (pointer_filter_verbose) {
173                     fprintf(stderr,"*Wf2: %p %p %p\n",
174                             pointer, start_addr, (void *)*start_addr);
175                 }
176                 return 0;
177             }
178             break;
179         default:
180             if (pointer_filter_verbose) {
181                 fprintf(stderr,"*Wf3: %p %p %p\n",
182                         pointer, start_addr, (void *)*start_addr);
183             }
184             return 0;
185         }
186         break;
187     case LIST_POINTER_LOWTAG:
188         if ((long)pointer != ((long)start_addr+LIST_POINTER_LOWTAG)) {
189             if (pointer_filter_verbose)
190                 fprintf(stderr,"*Wl1: %p %p %p\n",
191                         pointer, start_addr, (void *)*start_addr);
192             return 0;
193         }
194         /* Is it plausible cons? */
195         if ((is_lisp_pointer(start_addr[0])
196             || ((start_addr[0] & FIXNUM_TAG_MASK) == 0) /* fixnum */
197             || (widetag_of(start_addr[0]) == CHARACTER_WIDETAG)
198 #if N_WORD_BITS == 64
199             || (widetag_of(start_addr[0]) == SINGLE_FLOAT_WIDETAG)
200 #endif
201             || (widetag_of(start_addr[0]) == UNBOUND_MARKER_WIDETAG))
202            && (is_lisp_pointer(start_addr[1])
203                || ((start_addr[1] & FIXNUM_TAG_MASK) == 0) /* fixnum */
204                || (widetag_of(start_addr[1]) == CHARACTER_WIDETAG)
205 #if N_WORD_BITS == 64
206                || (widetag_of(start_addr[1]) == SINGLE_FLOAT_WIDETAG)
207 #endif
208                || (widetag_of(start_addr[1]) == UNBOUND_MARKER_WIDETAG))) {
209             break;
210         } else {
211             if (pointer_filter_verbose) {
212                 fprintf(stderr,"*Wl2: %p %p %p\n",
213                         pointer, start_addr, (void *)*start_addr);
214             }
215             return 0;
216         }
217     case INSTANCE_POINTER_LOWTAG:
218         if ((long)pointer != ((long)start_addr+INSTANCE_POINTER_LOWTAG)) {
219             if (pointer_filter_verbose) {
220                 fprintf(stderr,"*Wi1: %p %p %p\n",
221                         pointer, start_addr, (void *)*start_addr);
222             }
223             return 0;
224         }
225         if (widetag_of(start_addr[0]) != INSTANCE_HEADER_WIDETAG) {
226             if (pointer_filter_verbose) {
227                 fprintf(stderr,"*Wi2: %p %p %p\n",
228                         pointer, start_addr, (void *)*start_addr);
229             }
230             return 0;
231         }
232         break;
233     case OTHER_POINTER_LOWTAG:
234         if ((long)pointer != ((long)start_addr+OTHER_POINTER_LOWTAG)) {
235             if (pointer_filter_verbose) {
236                 fprintf(stderr,"*Wo1: %p %p %p\n",
237                         pointer, start_addr, (void *)*start_addr);
238             }
239             return 0;
240         }
241         /* Is it plausible? Not a cons. XXX should check the headers. */
242         if (is_lisp_pointer(start_addr[0]) || ((start_addr[0] & FIXNUM_TAG_MASK) == 0)) {
243             if (pointer_filter_verbose) {
244                 fprintf(stderr,"*Wo2: %p %p %p\n",
245                         pointer, start_addr, (void *)*start_addr);
246             }
247             return 0;
248         }
249         switch (widetag_of(start_addr[0])) {
250         case UNBOUND_MARKER_WIDETAG:
251         case CHARACTER_WIDETAG:
252 #if N_WORD_BITS == 64
253         case SINGLE_FLOAT_WIDETAG:
254 #endif
255             if (pointer_filter_verbose) {
256                 fprintf(stderr,"*Wo3: %p %p %p\n",
257                         pointer, start_addr, (void *)*start_addr);
258             }
259             return 0;
260
261             /* only pointed to by function pointers? */
262         case CLOSURE_HEADER_WIDETAG:
263         case FUNCALLABLE_INSTANCE_HEADER_WIDETAG:
264             if (pointer_filter_verbose) {
265                 fprintf(stderr,"*Wo4: %p %p %p\n",
266                         pointer, start_addr, (void *)*start_addr);
267             }
268             return 0;
269
270         case INSTANCE_HEADER_WIDETAG:
271             if (pointer_filter_verbose) {
272                 fprintf(stderr,"*Wo5: %p %p %p\n",
273                         pointer, start_addr, (void *)*start_addr);
274             }
275             return 0;
276
277             /* the valid other immediate pointer objects */
278         case SIMPLE_VECTOR_WIDETAG:
279         case RATIO_WIDETAG:
280         case COMPLEX_WIDETAG:
281 #ifdef COMPLEX_SINGLE_FLOAT_WIDETAG
282         case COMPLEX_SINGLE_FLOAT_WIDETAG:
283 #endif
284 #ifdef COMPLEX_DOUBLE_FLOAT_WIDETAG
285         case COMPLEX_DOUBLE_FLOAT_WIDETAG:
286 #endif
287 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
288         case COMPLEX_LONG_FLOAT_WIDETAG:
289 #endif
290         case SIMPLE_ARRAY_WIDETAG:
291         case COMPLEX_BASE_STRING_WIDETAG:
292 #ifdef COMPLEX_CHARACTER_STRING_WIDETAG
293         case COMPLEX_CHARACTER_STRING_WIDETAG:
294 #endif
295         case COMPLEX_VECTOR_NIL_WIDETAG:
296         case COMPLEX_BIT_VECTOR_WIDETAG:
297         case COMPLEX_VECTOR_WIDETAG:
298         case COMPLEX_ARRAY_WIDETAG:
299         case VALUE_CELL_HEADER_WIDETAG:
300         case SYMBOL_HEADER_WIDETAG:
301         case FDEFN_WIDETAG:
302         case CODE_HEADER_WIDETAG:
303         case BIGNUM_WIDETAG:
304 #if N_WORD_BITS != 64
305         case SINGLE_FLOAT_WIDETAG:
306 #endif
307         case DOUBLE_FLOAT_WIDETAG:
308 #ifdef LONG_FLOAT_WIDETAG
309         case LONG_FLOAT_WIDETAG:
310 #endif
311         case SIMPLE_ARRAY_NIL_WIDETAG:
312         case SIMPLE_BASE_STRING_WIDETAG:
313 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
314         case SIMPLE_CHARACTER_STRING_WIDETAG:
315 #endif
316         case SIMPLE_BIT_VECTOR_WIDETAG:
317         case SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG:
318         case SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG:
319         case SIMPLE_ARRAY_UNSIGNED_BYTE_7_WIDETAG:
320         case SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG:
321         case SIMPLE_ARRAY_UNSIGNED_BYTE_15_WIDETAG:
322         case SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG:
323 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_29_WIDETAG
324         case SIMPLE_ARRAY_UNSIGNED_BYTE_29_WIDETAG:
325 #endif
326         case SIMPLE_ARRAY_UNSIGNED_BYTE_31_WIDETAG:
327         case SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG:
328 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_60_WIDETAG
329                 case SIMPLE_ARRAY_UNSIGNED_BYTE_60_WIDETAG:
330 #endif
331 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG
332                 case SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG:
333 #endif
334 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG
335                 case SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG:
336 #endif
337 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
338         case SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG:
339 #endif
340 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
341         case SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG:
342 #endif
343 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG
344         case SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG:
345 #endif
346 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
347         case SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG:
348 #endif
349 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_61_WIDETAG
350                 case SIMPLE_ARRAY_SIGNED_BYTE_61_WIDETAG:
351 #endif
352 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG
353                 case SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG:
354 #endif
355         case SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG:
356         case SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG:
357 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
358         case SIMPLE_ARRAY_LONG_FLOAT_WIDETAG:
359 #endif
360 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
361         case SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG:
362 #endif
363 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
364         case SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG:
365 #endif
366 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
367         case SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG:
368 #endif
369         case SAP_WIDETAG:
370         case WEAK_POINTER_WIDETAG:
371 #ifdef LUTEX_WIDETAG
372         case LUTEX_WIDETAG:
373 #endif
374             break;
375
376         default:
377             if (pointer_filter_verbose) {
378                 fprintf(stderr,"*Wo6: %p %p %p\n",
379                         pointer, start_addr, (void *)*start_addr);
380             }
381             return 0;
382         }
383         break;
384     default:
385         if (pointer_filter_verbose) {
386             fprintf(stderr,"*W?: %p %p %p\n",
387                     pointer, start_addr, (void *)*start_addr);
388         }
389         return 0;
390     }
391
392     /* looks good */
393     return 1;
394 }
395
396 #define MAX_STACK_POINTERS 256
397 lispobj *valid_stack_locations[MAX_STACK_POINTERS];
398 unsigned long num_valid_stack_locations;
399
400 #define MAX_STACK_RETURN_ADDRESSES 128
401 lispobj *valid_stack_ra_locations[MAX_STACK_RETURN_ADDRESSES];
402 lispobj *valid_stack_ra_code_objects[MAX_STACK_RETURN_ADDRESSES];
403 unsigned long num_valid_stack_ra_locations;
404
405 /* Identify valid stack slots. */
406 static void
407 setup_i386_stack_scav(lispobj *lowaddr, lispobj *base)
408 {
409     lispobj *sp = lowaddr;
410     num_valid_stack_locations = 0;
411     num_valid_stack_ra_locations = 0;
412     for (sp = lowaddr; sp < base; sp++) {
413         lispobj thing = *sp;
414         /* Find the object start address */
415         lispobj *start_addr = search_dynamic_space((void *)thing);
416         if (start_addr) {
417             /* We need to allow raw pointers into Code objects for
418              * return addresses. This will also pick up pointers to
419              * functions in code objects. */
420             if (widetag_of(*start_addr) == CODE_HEADER_WIDETAG) {
421                 /* FIXME asserting here is a really dumb thing to do.
422                  * If we've overflowed some arbitrary static limit, we
423                  * should just refuse to purify, instead of killing
424                  * the whole lisp session
425                  */
426                 gc_assert(num_valid_stack_ra_locations <
427                           MAX_STACK_RETURN_ADDRESSES);
428                 valid_stack_ra_locations[num_valid_stack_ra_locations] = sp;
429                 valid_stack_ra_code_objects[num_valid_stack_ra_locations++] =
430                     (lispobj *)((long)start_addr + OTHER_POINTER_LOWTAG);
431             } else {
432                 if (valid_dynamic_space_pointer((void *)thing, start_addr)) {
433                     gc_assert(num_valid_stack_locations < MAX_STACK_POINTERS);
434                     valid_stack_locations[num_valid_stack_locations++] = sp;
435                 }
436             }
437         }
438     }
439     if (pointer_filter_verbose) {
440         fprintf(stderr, "number of valid stack pointers = %ld\n",
441                 num_valid_stack_locations);
442         fprintf(stderr, "number of stack return addresses = %ld\n",
443                 num_valid_stack_ra_locations);
444     }
445 }
446
447 static void
448 pscav_i386_stack(void)
449 {
450     long i;
451
452     for (i = 0; i < num_valid_stack_locations; i++)
453         pscav(valid_stack_locations[i], 1, 0);
454
455     for (i = 0; i < num_valid_stack_ra_locations; i++) {
456         lispobj code_obj = (lispobj)valid_stack_ra_code_objects[i];
457         pscav(&code_obj, 1, 0);
458         if (pointer_filter_verbose) {
459             fprintf(stderr,"*C moved RA %p to %p; for code object %p to %p\n",
460                     (void *)*valid_stack_ra_locations[i],
461                     (void *)(*valid_stack_ra_locations[i]) -
462                     ((void *)valid_stack_ra_code_objects[i] -
463                      (void *)code_obj),
464                     valid_stack_ra_code_objects[i], (void *)code_obj);
465         }
466         *valid_stack_ra_locations[i] =
467             ((long)(*valid_stack_ra_locations[i])
468              - ((long)valid_stack_ra_code_objects[i] - (long)code_obj));
469     }
470 }
471 #endif
472 #endif
473
474 \f
475 static void
476 pscav_later(lispobj *where, long count)
477 {
478     struct later *new;
479
480     if (count > LATERMAXCOUNT) {
481         while (count > LATERMAXCOUNT) {
482             pscav_later(where, LATERMAXCOUNT);
483             count -= LATERMAXCOUNT;
484             where += LATERMAXCOUNT;
485         }
486     }
487     else {
488         if (later_blocks == NULL || later_count == LATERBLOCKSIZE ||
489             (later_count == LATERBLOCKSIZE-1 && count > 1)) {
490             new  = (struct later *)malloc(sizeof(struct later));
491             new->next = later_blocks;
492             if (later_blocks && later_count < LATERBLOCKSIZE)
493                 later_blocks->u[later_count].ptr = NULL;
494             later_blocks = new;
495             later_count = 0;
496         }
497
498         if (count != 1)
499             later_blocks->u[later_count++].count = count;
500         later_blocks->u[later_count++].ptr = where;
501     }
502 }
503
504 static lispobj
505 ptrans_boxed(lispobj thing, lispobj header, boolean constant)
506 {
507     long nwords;
508     lispobj result, *new, *old;
509
510     nwords = CEILING(1 + HeaderValue(header), 2);
511
512     /* Allocate it */
513     old = (lispobj *)native_pointer(thing);
514     new = newspace_alloc(nwords,constant);
515
516     /* Copy it. */
517     bcopy(old, new, nwords * sizeof(lispobj));
518
519     /* Deposit forwarding pointer. */
520     result = make_lispobj(new, lowtag_of(thing));
521     *old = result;
522
523     /* Scavenge it. */
524     pscav(new, nwords, constant);
525
526     return result;
527 }
528
529 /* We need to look at the layout to see whether it is a pure structure
530  * class, and only then can we transport as constant. If it is pure,
531  * we can ALWAYS transport as a constant. */
532 static lispobj
533 ptrans_instance(lispobj thing, lispobj header, boolean /* ignored */ constant)
534 {
535     struct layout *layout =
536       (struct layout *) native_pointer(((struct instance *)native_pointer(thing))->slots[0]);
537     lispobj pure = layout->pure;
538
539     switch (pure) {
540     case T:
541         return (ptrans_boxed(thing, header, 1));
542     case NIL:
543         return (ptrans_boxed(thing, header, 0));
544     case 0:
545         {
546             /* Substructure: special case for the COMPACT-INFO-ENVs,
547              * where the instance may have a point to the dynamic
548              * space placed into it (e.g. the cache-name slot), but
549              * the lists and arrays at the time of a purify can be
550              * moved to the RO space. */
551             long nwords;
552             lispobj result, *new, *old;
553
554             nwords = CEILING(1 + HeaderValue(header), 2);
555
556             /* Allocate it */
557             old = (lispobj *)native_pointer(thing);
558             new = newspace_alloc(nwords, 0); /*  inconstant */
559
560             /* Copy it. */
561             bcopy(old, new, nwords * sizeof(lispobj));
562
563             /* Deposit forwarding pointer. */
564             result = make_lispobj(new, lowtag_of(thing));
565             *old = result;
566
567             /* Scavenge it. */
568             pscav(new, nwords, 1);
569
570             return result;
571         }
572     default:
573         gc_abort();
574         return NIL; /* dummy value: return something ... */
575     }
576 }
577
578 static lispobj
579 ptrans_fdefn(lispobj thing, lispobj header)
580 {
581     long nwords;
582     lispobj result, *new, *old, oldfn;
583     struct fdefn *fdefn;
584
585     nwords = CEILING(1 + HeaderValue(header), 2);
586
587     /* Allocate it */
588     old = (lispobj *)native_pointer(thing);
589     new = newspace_alloc(nwords, 0);    /* inconstant */
590
591     /* Copy it. */
592     bcopy(old, new, nwords * sizeof(lispobj));
593
594     /* Deposit forwarding pointer. */
595     result = make_lispobj(new, lowtag_of(thing));
596     *old = result;
597
598     /* Scavenge the function. */
599     fdefn = (struct fdefn *)new;
600     oldfn = fdefn->fun;
601     pscav(&fdefn->fun, 1, 0);
602     if ((char *)oldfn + FUN_RAW_ADDR_OFFSET == fdefn->raw_addr)
603         fdefn->raw_addr = (char *)fdefn->fun + FUN_RAW_ADDR_OFFSET;
604
605     return result;
606 }
607
608 static lispobj
609 ptrans_unboxed(lispobj thing, lispobj header)
610 {
611     long nwords;
612     lispobj result, *new, *old;
613
614     nwords = CEILING(1 + HeaderValue(header), 2);
615
616     /* Allocate it */
617     old = (lispobj *)native_pointer(thing);
618     new = newspace_alloc(nwords,1);     /* always constant */
619
620     /* copy it. */
621     bcopy(old, new, nwords * sizeof(lispobj));
622
623     /* Deposit forwarding pointer. */
624     result = make_lispobj(new , lowtag_of(thing));
625     *old = result;
626
627     return result;
628 }
629
630 static lispobj
631 ptrans_vector(lispobj thing, long bits, long extra,
632               boolean boxed, boolean constant)
633 {
634     struct vector *vector;
635     long nwords;
636     lispobj result, *new;
637     long length;
638
639     vector = (struct vector *)native_pointer(thing);
640     length = fixnum_value(vector->length)+extra;
641     // Argh, handle simple-vector-nil separately.
642     if (bits == 0) {
643       nwords = 2;
644     } else {
645       nwords = CEILING(NWORDS(length, bits) + 2, 2);
646     }
647
648     new=newspace_alloc(nwords, (constant || !boxed));
649     bcopy(vector, new, nwords * sizeof(lispobj));
650
651     result = make_lispobj(new, lowtag_of(thing));
652     vector->header = result;
653
654     if (boxed)
655         pscav(new, nwords, constant);
656
657     return result;
658 }
659
660 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
661 static void
662 apply_code_fixups_during_purify(struct code *old_code, struct code *new_code)
663 {
664     long nheader_words, ncode_words, nwords;
665     void  *constants_start_addr, *constants_end_addr;
666     void  *code_start_addr, *code_end_addr;
667     lispobj fixups = NIL;
668     unsigned  displacement = (unsigned)new_code - (unsigned)old_code;
669     struct vector *fixups_vector;
670
671     ncode_words = fixnum_value(new_code->code_size);
672     nheader_words = HeaderValue(*(lispobj *)new_code);
673     nwords = ncode_words + nheader_words;
674
675     constants_start_addr = (void *)new_code + 5 * N_WORD_BYTES;
676     constants_end_addr = (void *)new_code + nheader_words*N_WORD_BYTES;
677     code_start_addr = (void *)new_code + nheader_words*N_WORD_BYTES;
678     code_end_addr = (void *)new_code + nwords*N_WORD_BYTES;
679
680     /* The first constant should be a pointer to the fixups for this
681      * code objects. Check. */
682     fixups = new_code->constants[0];
683
684     /* It will be 0 or the unbound-marker if there are no fixups, and
685      * will be an other-pointer to a vector if it is valid. */
686     if ((fixups==0) ||
687         (fixups==UNBOUND_MARKER_WIDETAG) ||
688         !is_lisp_pointer(fixups)) {
689 #ifdef LISP_FEATURE_GENCGC
690         /* Check for a possible errors. */
691         sniff_code_object(new_code,displacement);
692 #endif
693         return;
694     }
695
696     fixups_vector = (struct vector *)native_pointer(fixups);
697
698     /* Could be pointing to a forwarding pointer. */
699     if (is_lisp_pointer(fixups) && (dynamic_pointer_p(fixups))
700         && forwarding_pointer_p(*(lispobj *)fixups_vector)) {
701         /* If so then follow it. */
702         fixups_vector =
703             (struct vector *)native_pointer(*(lispobj *)fixups_vector);
704     }
705
706     if (widetag_of(fixups_vector->header) == SIMPLE_ARRAY_WORD_WIDETAG) {
707         /* We got the fixups for the code block. Now work through the
708          * vector, and apply a fixup at each address. */
709         long length = fixnum_value(fixups_vector->length);
710         long i;
711         for (i=0; i<length; i++) {
712             unsigned offset = fixups_vector->data[i];
713             /* Now check the current value of offset. */
714             unsigned old_value =
715                 *(unsigned *)((unsigned)code_start_addr + offset);
716
717             /* If it's within the old_code object then it must be an
718              * absolute fixup (relative ones are not saved) */
719             if ((old_value>=(unsigned)old_code)
720                 && (old_value<((unsigned)old_code + nwords * N_WORD_BYTES)))
721                 /* So add the dispacement. */
722                 *(unsigned *)((unsigned)code_start_addr + offset) = old_value
723                     + displacement;
724             else
725                 /* It is outside the old code object so it must be a relative
726                  * fixup (absolute fixups are not saved). So subtract the
727                  * displacement. */
728                 *(unsigned *)((unsigned)code_start_addr + offset) = old_value
729                     - displacement;
730         }
731     }
732
733     /* No longer need the fixups. */
734     new_code->constants[0] = 0;
735
736 #ifdef LISP_FEATURE_GENCGC
737     /* Check for possible errors. */
738     sniff_code_object(new_code,displacement);
739 #endif
740 }
741 #endif
742
743 static lispobj
744 ptrans_code(lispobj thing)
745 {
746     struct code *code, *new;
747     long nwords;
748     lispobj func, result;
749
750     code = (struct code *)native_pointer(thing);
751     nwords = CEILING(HeaderValue(code->header) + fixnum_value(code->code_size),
752                      2);
753
754     new = (struct code *)newspace_alloc(nwords,1); /* constant */
755
756     bcopy(code, new, nwords * sizeof(lispobj));
757
758 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
759     apply_code_fixups_during_purify(code,new);
760 #endif
761
762     result = make_lispobj(new, OTHER_POINTER_LOWTAG);
763
764     /* Stick in a forwarding pointer for the code object. */
765     *(lispobj *)code = result;
766
767     /* Put in forwarding pointers for all the functions. */
768     for (func = code->entry_points;
769          func != NIL;
770          func = ((struct simple_fun *)native_pointer(func))->next) {
771
772         gc_assert(lowtag_of(func) == FUN_POINTER_LOWTAG);
773
774         *(lispobj *)native_pointer(func) = result + (func - thing);
775     }
776
777     /* Arrange to scavenge the debug info later. */
778     pscav_later(&new->debug_info, 1);
779
780     /* FIXME: why would this be a fixnum? */
781     /* "why" is a hard word, but apparently for compiled functions the
782        trace_table_offset contains the length of the instructions, as
783        a fixnum.  See CODE-INST-AREA-LENGTH in
784        src/compiler/target-disassem.lisp.  -- CSR, 2004-01-08 */
785     if (!(fixnump(new->trace_table_offset)))
786 #if 0
787         pscav(&new->trace_table_offset, 1, 0);
788 #else
789         new->trace_table_offset = NIL; /* limit lifetime */
790 #endif
791
792     /* Scavenge the constants. */
793     pscav(new->constants, HeaderValue(new->header)-5, 1);
794
795     /* Scavenge all the functions. */
796     pscav(&new->entry_points, 1, 1);
797     for (func = new->entry_points;
798          func != NIL;
799          func = ((struct simple_fun *)native_pointer(func))->next) {
800         gc_assert(lowtag_of(func) == FUN_POINTER_LOWTAG);
801         gc_assert(!dynamic_pointer_p(func));
802
803 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
804         /* Temporarily convert the self pointer to a real function pointer. */
805         ((struct simple_fun *)native_pointer(func))->self
806             -= FUN_RAW_ADDR_OFFSET;
807 #endif
808         pscav(&((struct simple_fun *)native_pointer(func))->self, 2, 1);
809 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
810         ((struct simple_fun *)native_pointer(func))->self
811             += FUN_RAW_ADDR_OFFSET;
812 #endif
813         pscav_later(&((struct simple_fun *)native_pointer(func))->name, 3);
814     }
815
816     return result;
817 }
818
819 static lispobj
820 ptrans_func(lispobj thing, lispobj header)
821 {
822     long nwords;
823     lispobj code, *new, *old, result;
824     struct simple_fun *function;
825
826     /* Thing can either be a function header, a closure function
827      * header, a closure, or a funcallable-instance. If it's a closure
828      * or a funcallable-instance, we do the same as ptrans_boxed.
829      * Otherwise we have to do something strange, 'cause it is buried
830      * inside a code object. */
831
832     if (widetag_of(header) == SIMPLE_FUN_HEADER_WIDETAG) {
833
834         /* We can only end up here if the code object has not been
835          * scavenged, because if it had been scavenged, forwarding pointers
836          * would have been left behind for all the entry points. */
837
838         function = (struct simple_fun *)native_pointer(thing);
839         code =
840             make_lispobj
841             ((native_pointer(thing) -
842               (HeaderValue(function->header))), OTHER_POINTER_LOWTAG);
843
844         /* This will cause the function's header to be replaced with a
845          * forwarding pointer. */
846
847         ptrans_code(code);
848
849         /* So we can just return that. */
850         return function->header;
851     }
852     else {
853         /* It's some kind of closure-like thing. */
854         nwords = CEILING(1 + HeaderValue(header), 2);
855         old = (lispobj *)native_pointer(thing);
856
857         /* Allocate the new one.  FINs *must* not go in read_only
858          * space.  Closures can; they never change */
859
860         new = newspace_alloc
861             (nwords,(widetag_of(header)!=FUNCALLABLE_INSTANCE_HEADER_WIDETAG));
862
863         /* Copy it. */
864         bcopy(old, new, nwords * sizeof(lispobj));
865
866         /* Deposit forwarding pointer. */
867         result = make_lispobj(new, lowtag_of(thing));
868         *old = result;
869
870         /* Scavenge it. */
871         pscav(new, nwords, 0);
872
873         return result;
874     }
875 }
876
877 static lispobj
878 ptrans_returnpc(lispobj thing, lispobj header)
879 {
880     lispobj code, new;
881
882     /* Find the corresponding code object. */
883     code = thing - HeaderValue(header)*sizeof(lispobj);
884
885     /* Make sure it's been transported. */
886     new = *(lispobj *)native_pointer(code);
887     if (!forwarding_pointer_p(new))
888         new = ptrans_code(code);
889
890     /* Maintain the offset: */
891     return new + (thing - code);
892 }
893
894 #define WORDS_PER_CONS CEILING(sizeof(struct cons) / sizeof(lispobj), 2)
895
896 static lispobj
897 ptrans_list(lispobj thing, boolean constant)
898 {
899     struct cons *old, *new, *orig;
900     long length;
901
902     orig = (struct cons *) newspace_alloc(0,constant);
903     length = 0;
904
905     do {
906         /* Allocate a new cons cell. */
907         old = (struct cons *)native_pointer(thing);
908         new = (struct cons *) newspace_alloc(WORDS_PER_CONS,constant);
909
910         /* Copy the cons cell and keep a pointer to the cdr. */
911         new->car = old->car;
912         thing = new->cdr = old->cdr;
913
914         /* Set up the forwarding pointer. */
915         *(lispobj *)old = make_lispobj(new, LIST_POINTER_LOWTAG);
916
917         /* And count this cell. */
918         length++;
919     } while (lowtag_of(thing) == LIST_POINTER_LOWTAG &&
920              dynamic_pointer_p(thing) &&
921              !(forwarding_pointer_p(*(lispobj *)native_pointer(thing))));
922
923     /* Scavenge the list we just copied. */
924     pscav((lispobj *)orig, length * WORDS_PER_CONS, constant);
925
926     return make_lispobj(orig, LIST_POINTER_LOWTAG);
927 }
928
929 static lispobj
930 ptrans_otherptr(lispobj thing, lispobj header, boolean constant)
931 {
932     switch (widetag_of(header)) {
933         /* FIXME: this needs a reindent */
934       case BIGNUM_WIDETAG:
935       case SINGLE_FLOAT_WIDETAG:
936       case DOUBLE_FLOAT_WIDETAG:
937 #ifdef LONG_FLOAT_WIDETAG
938       case LONG_FLOAT_WIDETAG:
939 #endif
940 #ifdef COMPLEX_SINGLE_FLOAT_WIDETAG
941       case COMPLEX_SINGLE_FLOAT_WIDETAG:
942 #endif
943 #ifdef COMPLEX_DOUBLE_FLOAT_WIDETAG
944       case COMPLEX_DOUBLE_FLOAT_WIDETAG:
945 #endif
946 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
947       case COMPLEX_LONG_FLOAT_WIDETAG:
948 #endif
949       case SAP_WIDETAG:
950           return ptrans_unboxed(thing, header);
951 #ifdef LUTEX_WIDETAG
952       case LUTEX_WIDETAG:
953           gencgc_unregister_lutex(native_pointer(thing));
954           return ptrans_unboxed(thing, header);
955 #endif
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                 /* 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_purify_pointer =
1456       (lispobj*)SymbolValue(ALLOCATION_POINTER,0);
1457 #else
1458 #if defined(LISP_FEATURE_GENCGC)
1459     dynamic_space_purify_pointer = get_alloc_pointer();
1460 #else
1461     dynamic_space_purify_pointer = dynamic_space_free_pointer;
1462 #endif
1463 #endif
1464
1465     read_only_end = read_only_free =
1466         (lispobj *)SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0);
1467     static_end = static_free =
1468         (lispobj *)SymbolValue(STATIC_SPACE_FREE_POINTER,0);
1469
1470 #ifdef PRINTNOISE
1471     printf(" roots");
1472     fflush(stdout);
1473 #endif
1474
1475 #if defined(LISP_FEATURE_GENCGC) && (defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64))
1476     /* note this expects only one thread to be active.  We'd have to
1477      * stop all the others in the same way as GC does if we wanted
1478      * PURIFY to work when >1 thread exists */
1479     setup_i386_stack_scav(((&static_roots)-2),
1480                           ((void *)all_threads->control_stack_end));
1481 #endif
1482
1483     pscav(&static_roots, 1, 0);
1484     pscav(&read_only_roots, 1, 1);
1485
1486 #ifdef PRINTNOISE
1487     printf(" handlers");
1488     fflush(stdout);
1489 #endif
1490     pscav((lispobj *) interrupt_handlers,
1491           sizeof(interrupt_handlers) / sizeof(lispobj),
1492           0);
1493
1494 #ifdef PRINTNOISE
1495     printf(" stack");
1496     fflush(stdout);
1497 #endif
1498 #if !defined(LISP_FEATURE_X86) && !defined(LISP_FEATURE_X86_64)
1499     pscav((lispobj *)all_threads->control_stack_start,
1500           current_control_stack_pointer -
1501           all_threads->control_stack_start,
1502           0);
1503 #else
1504 #ifdef LISP_FEATURE_GENCGC
1505     pscav_i386_stack();
1506 #endif
1507 #endif
1508
1509 #ifdef PRINTNOISE
1510     printf(" bindings");
1511     fflush(stdout);
1512 #endif
1513 #if !(defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64))
1514     pscav( (lispobj *)all_threads->binding_stack_start,
1515           (lispobj *)current_binding_stack_pointer -
1516            all_threads->binding_stack_start,
1517           0);
1518 #else
1519     for_each_thread(thread) {
1520         pscav( (lispobj *)thread->binding_stack_start,
1521                (lispobj *)SymbolValue(BINDING_STACK_POINTER,thread) -
1522                (lispobj *)thread->binding_stack_start,
1523           0);
1524 #ifdef LISP_FEATURE_SB_THREAD
1525         pscav( (lispobj *) (thread+1),
1526                fixnum_value(SymbolValue(FREE_TLS_INDEX,0)) -
1527                (sizeof (struct thread))/(sizeof (lispobj)),
1528           0);
1529 #endif
1530     }
1531
1532
1533 #endif
1534
1535     /* The original CMU CL code had scavenge-read-only-space code
1536      * controlled by the Lisp-level variable
1537      * *SCAVENGE-READ-ONLY-SPACE*. It was disabled by default, and it
1538      * wasn't documented under what circumstances it was useful or
1539      * safe to turn it on, so it's been turned off in SBCL. If you
1540      * want/need this functionality, and can test and document it,
1541      * please submit a patch. */
1542 #if 0
1543     if (SymbolValue(SCAVENGE_READ_ONLY_SPACE) != UNBOUND_MARKER_WIDETAG
1544         && SymbolValue(SCAVENGE_READ_ONLY_SPACE) != NIL) {
1545       unsigned  read_only_space_size =
1546           (lispobj *)SymbolValue(READ_ONLY_SPACE_FREE_POINTER) -
1547           (lispobj *)READ_ONLY_SPACE_START;
1548       fprintf(stderr,
1549               "scavenging read only space: %d bytes\n",
1550               read_only_space_size * sizeof(lispobj));
1551       pscav( (lispobj *)READ_ONLY_SPACE_START, read_only_space_size, 0);
1552     }
1553 #endif
1554
1555 #ifdef PRINTNOISE
1556     printf(" static");
1557     fflush(stdout);
1558 #endif
1559     clean = (lispobj *)STATIC_SPACE_START;
1560     do {
1561         while (clean != static_free)
1562             clean = pscav(clean, static_free - clean, 0);
1563         laters = later_blocks;
1564         count = later_count;
1565         later_blocks = NULL;
1566         later_count = 0;
1567         while (laters != NULL) {
1568             for (i = 0; i < count; i++) {
1569                 if (laters->u[i].count == 0) {
1570                     ;
1571                 } else if (laters->u[i].count <= LATERMAXCOUNT) {
1572                     pscav(laters->u[i+1].ptr, laters->u[i].count, 1);
1573                     i++;
1574                 } else {
1575                     pscav(laters->u[i].ptr, 1, 1);
1576                 }
1577             }
1578             next = laters->next;
1579             free(laters);
1580             laters = next;
1581             count = LATERBLOCKSIZE;
1582         }
1583     } while (clean != static_free || later_blocks != NULL);
1584
1585 #ifdef PRINTNOISE
1586     printf(" cleanup");
1587     fflush(stdout);
1588 #endif
1589
1590     os_zero((os_vm_address_t) current_dynamic_space,
1591             (os_vm_size_t) dynamic_space_size);
1592
1593     /* Zero the stack. Note that the stack is also zeroed by SUB-GC
1594      * calling SCRUB-CONTROL-STACK - this zeros the stack on the x86. */
1595 #if !defined(LISP_FEATURE_X86) && !defined(LISP_FEATURE_X86_64)
1596     os_zero((os_vm_address_t) current_control_stack_pointer,
1597             (os_vm_size_t)
1598             ((all_threads->control_stack_end -
1599               current_control_stack_pointer) * sizeof(lispobj)));
1600 #endif
1601
1602     /* It helps to update the heap free pointers so that free_heap can
1603      * verify after it's done. */
1604     SetSymbolValue(READ_ONLY_SPACE_FREE_POINTER, (lispobj)read_only_free,0);
1605     SetSymbolValue(STATIC_SPACE_FREE_POINTER, (lispobj)static_free,0);
1606
1607 #if defined LISP_FEATURE_GENCGC
1608     gc_free_heap();
1609 #else
1610     dynamic_space_free_pointer = current_dynamic_space;
1611     set_auto_gc_trigger(bytes_consed_between_gcs);
1612 #endif
1613
1614     /* Blast away instruction cache */
1615     os_flush_icache((os_vm_address_t)READ_ONLY_SPACE_START, READ_ONLY_SPACE_SIZE);
1616     os_flush_icache((os_vm_address_t)STATIC_SPACE_START, STATIC_SPACE_SIZE);
1617
1618 #ifdef PRINTNOISE
1619     printf(" done]\n");
1620     fflush(stdout);
1621 #endif
1622     return 0;
1623 }