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