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