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