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