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