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