2 * C-level stuff to implement Lisp-level PURIFY
6 * This software is part of the SBCL system. See the README file for
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.
17 #include <sys/types.h>
27 #include "interrupt.h"
32 #include "gc-internal.h"
34 #include "genesis/primitive-objects.h"
35 #include "genesis/static-symbols.h"
39 #if defined(LISP_FEATURE_GENCGC)
40 /* this is another artifact of the poor integration between gencgc and
41 * the rest of the runtime: on cheney gc there is a global
42 * dynamic_space_free_pointer which is valid whenever foreign function
43 * call is active, but in gencgc there's no such variable and we have
46 static lispobj *dynamic_space_free_pointer;
48 extern unsigned long bytes_consed_between_gcs;
51 lose("GC invariant lost, file \"%s\", line %d", __FILE__, __LINE__)
54 #define gc_assert(ex) do { \
55 if (!(ex)) gc_abort(); \
62 /* These hold the original end of the read_only and static spaces so
63 * we can tell what are forwarding pointers. */
65 static lispobj *read_only_end, *static_end;
67 static lispobj *read_only_free, *static_free;
69 static lispobj *pscav(lispobj *addr, long nwords, boolean constant);
71 #define LATERBLOCKSIZE 1020
72 #define LATERMAXCOUNT 10
81 } *later_blocks = NULL;
82 static long later_count = 0;
85 #define SIMPLE_ARRAY_WORD_WIDETAG SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG
86 #elif N_WORD_BITS == 64
87 #define SIMPLE_ARRAY_WORD_WIDETAG SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG
90 /* FIXME: Shouldn't this be defined in sbcl.h? See also notes in
94 #define FUN_RAW_ADDR_OFFSET 0
96 #define FUN_RAW_ADDR_OFFSET (6*sizeof(lispobj) - FUN_POINTER_LOWTAG)
100 forwarding_pointer_p(lispobj obj)
102 lispobj *ptr = native_pointer(obj);
104 return ((static_end <= ptr && ptr <= static_free) ||
105 (read_only_end <= ptr && ptr <= read_only_free));
109 dynamic_pointer_p(lispobj ptr)
111 #ifndef LISP_FEATURE_GENCGC
112 return (ptr >= (lispobj)current_dynamic_space
114 ptr < (lispobj)dynamic_space_free_pointer);
116 /* Be more conservative, and remember, this is a maybe. */
117 return (ptr >= (lispobj)DYNAMIC_SPACE_START
119 ptr < (lispobj)dynamic_space_free_pointer);
123 static inline lispobj *
124 newspace_alloc(long nwords, int constantp)
127 nwords=CEILING(nwords,2);
130 read_only_free+=nwords;
140 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
142 #ifdef LISP_FEATURE_GENCGC
144 * enhanced x86/GENCGC stack scavenging by Douglas Crosher
146 * Scavenging the stack on the i386 is problematic due to conservative
147 * roots and raw return addresses. Here it is handled in two passes:
148 * the first pass runs before any objects are moved and tries to
149 * identify valid pointers and return address on the stack, the second
150 * pass scavenges these.
153 static unsigned pointer_filter_verbose = 0;
155 /* FIXME: This is substantially the same code as
156 * possibly_valid_dynamic_space_pointer in gencgc.c. The only
157 * relevant difference seems to be that the gencgc code also checks
158 * for raw pointers into Code objects, whereas in purify these are
159 * checked separately in setup_i386_stack_scav - they go onto
160 * valid_stack_ra_locations instead of just valid_stack_locations */
163 valid_dynamic_space_pointer(lispobj *pointer, lispobj *start_addr)
165 /* If it's not a return address then it needs to be a valid Lisp
167 if (!is_lisp_pointer((lispobj)pointer))
170 /* Check that the object pointed to is consistent with the pointer
172 switch (lowtag_of((lispobj)pointer)) {
173 case FUN_POINTER_LOWTAG:
174 /* Start_addr should be the enclosing code object, or a closure
176 switch (widetag_of(*start_addr)) {
177 case CODE_HEADER_WIDETAG:
178 /* This case is probably caught above. */
180 case CLOSURE_HEADER_WIDETAG:
181 case FUNCALLABLE_INSTANCE_HEADER_WIDETAG:
182 if ((long)pointer != ((long)start_addr+FUN_POINTER_LOWTAG)) {
183 if (pointer_filter_verbose) {
184 fprintf(stderr,"*Wf2: %x %x %x\n",
185 (unsigned long) pointer,
186 (unsigned long) start_addr, *start_addr);
192 if (pointer_filter_verbose) {
193 fprintf(stderr,"*Wf3: %x %x %x\n", (unsigned long) pointer,
194 (unsigned long) start_addr, *start_addr);
199 case LIST_POINTER_LOWTAG:
200 if ((long)pointer != ((long)start_addr+LIST_POINTER_LOWTAG)) {
201 if (pointer_filter_verbose)
202 fprintf(stderr,"*Wl1: %x %x %x\n", (unsigned long) pointer,
203 (unsigned long) start_addr, *start_addr);
206 /* Is it plausible cons? */
207 if ((is_lisp_pointer(start_addr[0])
208 || ((start_addr[0] & 3) == 0) /* fixnum */
209 || (widetag_of(start_addr[0]) == CHARACTER_WIDETAG)
210 || (widetag_of(start_addr[0]) == UNBOUND_MARKER_WIDETAG))
211 && (is_lisp_pointer(start_addr[1])
212 || ((start_addr[1] & 3) == 0) /* fixnum */
213 || (widetag_of(start_addr[1]) == CHARACTER_WIDETAG)
214 || (widetag_of(start_addr[1]) == UNBOUND_MARKER_WIDETAG))) {
217 if (pointer_filter_verbose) {
218 fprintf(stderr,"*Wl2: %x %x %x\n", (unsigned long) pointer,
219 (unsigned long) start_addr, *start_addr);
223 case INSTANCE_POINTER_LOWTAG:
224 if ((long)pointer != ((long)start_addr+INSTANCE_POINTER_LOWTAG)) {
225 if (pointer_filter_verbose) {
226 fprintf(stderr,"*Wi1: %x %x %x\n", (unsigned long) pointer,
227 (unsigned long) start_addr, *start_addr);
231 if (widetag_of(start_addr[0]) != INSTANCE_HEADER_WIDETAG) {
232 if (pointer_filter_verbose) {
233 fprintf(stderr,"*Wi2: %x %x %x\n", (unsigned long) pointer,
234 (unsigned long) start_addr, *start_addr);
239 case OTHER_POINTER_LOWTAG:
240 if ((long)pointer != ((long)start_addr+OTHER_POINTER_LOWTAG)) {
241 if (pointer_filter_verbose) {
242 fprintf(stderr,"*Wo1: %x %x %x\n", (unsigned long) pointer,
243 (unsigned long) start_addr, *start_addr);
247 /* Is it plausible? Not a cons. XXX should check the headers. */
248 if (is_lisp_pointer(start_addr[0]) || ((start_addr[0] & 3) == 0)) {
249 if (pointer_filter_verbose) {
250 fprintf(stderr,"*Wo2: %x %x %x\n", (unsigned long) pointer,
251 (unsigned long) start_addr, *start_addr);
255 switch (widetag_of(start_addr[0])) {
256 case UNBOUND_MARKER_WIDETAG:
257 case CHARACTER_WIDETAG:
258 if (pointer_filter_verbose) {
259 fprintf(stderr,"*Wo3: %x %x %x\n", (unsigned long) pointer,
260 (unsigned long) start_addr, *start_addr);
264 /* only pointed to by function pointers? */
265 case CLOSURE_HEADER_WIDETAG:
266 case FUNCALLABLE_INSTANCE_HEADER_WIDETAG:
267 if (pointer_filter_verbose) {
268 fprintf(stderr,"*Wo4: %x %x %x\n", (unsigned long) pointer,
269 (unsigned long) start_addr, *start_addr);
273 case INSTANCE_HEADER_WIDETAG:
274 if (pointer_filter_verbose) {
275 fprintf(stderr,"*Wo5: %x %x %x\n", (unsigned long) pointer,
276 (unsigned long) start_addr, *start_addr);
280 /* the valid other immediate pointer objects */
281 case SIMPLE_VECTOR_WIDETAG:
283 case COMPLEX_WIDETAG:
284 #ifdef COMPLEX_SINGLE_FLOAT_WIDETAG
285 case COMPLEX_SINGLE_FLOAT_WIDETAG:
287 #ifdef COMPLEX_DOUBLE_FLOAT_WIDETAG
288 case COMPLEX_DOUBLE_FLOAT_WIDETAG:
290 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
291 case COMPLEX_LONG_FLOAT_WIDETAG:
293 case SIMPLE_ARRAY_WIDETAG:
294 case COMPLEX_BASE_STRING_WIDETAG:
295 #ifdef COMPLEX_CHARACTER_STRING_WIDETAG
296 case COMPLEX_CHARACTER_STRING_WIDETAG:
298 case COMPLEX_VECTOR_NIL_WIDETAG:
299 case COMPLEX_BIT_VECTOR_WIDETAG:
300 case COMPLEX_VECTOR_WIDETAG:
301 case COMPLEX_ARRAY_WIDETAG:
302 case VALUE_CELL_HEADER_WIDETAG:
303 case SYMBOL_HEADER_WIDETAG:
305 case CODE_HEADER_WIDETAG:
307 case SINGLE_FLOAT_WIDETAG:
308 case DOUBLE_FLOAT_WIDETAG:
309 #ifdef LONG_FLOAT_WIDETAG
310 case LONG_FLOAT_WIDETAG:
312 case SIMPLE_ARRAY_NIL_WIDETAG:
313 case SIMPLE_BASE_STRING_WIDETAG:
314 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
315 case SIMPLE_CHARACTER_STRING_WIDETAG:
317 case SIMPLE_BIT_VECTOR_WIDETAG:
318 case SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG:
319 case SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG:
320 case SIMPLE_ARRAY_UNSIGNED_BYTE_7_WIDETAG:
321 case SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG:
322 case SIMPLE_ARRAY_UNSIGNED_BYTE_15_WIDETAG:
323 case SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG:
324 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_29_WIDETAG
325 case SIMPLE_ARRAY_UNSIGNED_BYTE_29_WIDETAG:
327 case SIMPLE_ARRAY_UNSIGNED_BYTE_31_WIDETAG:
328 case SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG:
329 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_60_WIDETAG
330 case SIMPLE_ARRAY_UNSIGNED_BYTE_60_WIDETAG:
332 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG
333 case SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG:
335 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG
336 case SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG:
338 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
339 case SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG:
341 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
342 case SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG:
344 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG
345 case SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG:
347 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
348 case SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG:
350 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_61_WIDETAG
351 case SIMPLE_ARRAY_SIGNED_BYTE_61_WIDETAG:
353 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG
354 case SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG:
356 case SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG:
357 case SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG:
358 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
359 case SIMPLE_ARRAY_LONG_FLOAT_WIDETAG:
361 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
362 case SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG:
364 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
365 case SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG:
367 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
368 case SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG:
371 case WEAK_POINTER_WIDETAG:
375 if (pointer_filter_verbose) {
376 fprintf(stderr,"*Wo6: %x %x %x\n", (unsigned long) pointer,
377 (unsigned long) start_addr, *start_addr);
383 if (pointer_filter_verbose) {
384 fprintf(stderr,"*W?: %x %x %x\n", (unsigned long) pointer,
385 (unsigned long) start_addr, *start_addr);
394 #define MAX_STACK_POINTERS 256
395 lispobj *valid_stack_locations[MAX_STACK_POINTERS];
396 unsigned long num_valid_stack_locations;
398 #define MAX_STACK_RETURN_ADDRESSES 128
399 lispobj *valid_stack_ra_locations[MAX_STACK_RETURN_ADDRESSES];
400 lispobj *valid_stack_ra_code_objects[MAX_STACK_RETURN_ADDRESSES];
401 unsigned long num_valid_stack_ra_locations;
403 /* Identify valid stack slots. */
405 setup_i386_stack_scav(lispobj *lowaddr, lispobj *base)
407 lispobj *sp = lowaddr;
408 num_valid_stack_locations = 0;
409 num_valid_stack_ra_locations = 0;
410 for (sp = lowaddr; sp < base; sp++) {
412 /* Find the object start address */
413 lispobj *start_addr = search_dynamic_space((void *)thing);
415 /* We need to allow raw pointers into Code objects for
416 * return addresses. This will also pick up pointers to
417 * functions in code objects. */
418 if (widetag_of(*start_addr) == CODE_HEADER_WIDETAG) {
419 /* FIXME asserting here is a really dumb thing to do.
420 * If we've overflowed some arbitrary static limit, we
421 * should just refuse to purify, instead of killing
422 * the whole lisp session
424 gc_assert(num_valid_stack_ra_locations <
425 MAX_STACK_RETURN_ADDRESSES);
426 valid_stack_ra_locations[num_valid_stack_ra_locations] = sp;
427 valid_stack_ra_code_objects[num_valid_stack_ra_locations++] =
428 (lispobj *)((long)start_addr + OTHER_POINTER_LOWTAG);
430 if (valid_dynamic_space_pointer((void *)thing, start_addr)) {
431 gc_assert(num_valid_stack_locations < MAX_STACK_POINTERS);
432 valid_stack_locations[num_valid_stack_locations++] = sp;
437 if (pointer_filter_verbose) {
438 fprintf(stderr, "number of valid stack pointers = %d\n",
439 num_valid_stack_locations);
440 fprintf(stderr, "number of stack return addresses = %d\n",
441 num_valid_stack_ra_locations);
446 pscav_i386_stack(void)
450 for (i = 0; i < num_valid_stack_locations; i++)
451 pscav(valid_stack_locations[i], 1, 0);
453 for (i = 0; i < num_valid_stack_ra_locations; i++) {
454 lispobj code_obj = (lispobj)valid_stack_ra_code_objects[i];
455 pscav(&code_obj, 1, 0);
456 if (pointer_filter_verbose) {
457 fprintf(stderr,"*C moved RA %x to %x; for code object %x to %x\n",
458 *valid_stack_ra_locations[i],
459 (long)(*valid_stack_ra_locations[i])
460 - ((long)valid_stack_ra_code_objects[i] - (long)code_obj),
461 (unsigned long) valid_stack_ra_code_objects[i], code_obj);
463 *valid_stack_ra_locations[i] =
464 ((long)(*valid_stack_ra_locations[i])
465 - ((long)valid_stack_ra_code_objects[i] - (long)code_obj));
473 pscav_later(lispobj *where, long count)
477 if (count > LATERMAXCOUNT) {
478 while (count > LATERMAXCOUNT) {
479 pscav_later(where, LATERMAXCOUNT);
480 count -= LATERMAXCOUNT;
481 where += LATERMAXCOUNT;
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;
496 later_blocks->u[later_count++].count = count;
497 later_blocks->u[later_count++].ptr = where;
502 ptrans_boxed(lispobj thing, lispobj header, boolean constant)
505 lispobj result, *new, *old;
507 nwords = CEILING(1 + HeaderValue(header), 2);
510 old = (lispobj *)native_pointer(thing);
511 new = newspace_alloc(nwords,constant);
514 bcopy(old, new, nwords * sizeof(lispobj));
516 /* Deposit forwarding pointer. */
517 result = make_lispobj(new, lowtag_of(thing));
521 pscav(new, nwords, constant);
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. */
530 ptrans_instance(lispobj thing, lispobj header, boolean /* ignored */ constant)
532 lispobj layout = ((struct instance *)native_pointer(thing))->slots[0];
533 lispobj pure = ((struct instance *)native_pointer(layout))->slots[15];
537 return (ptrans_boxed(thing, header, 1));
539 return (ptrans_boxed(thing, header, 0));
542 /* Substructure: special case for the COMPACT-INFO-ENVs,
543 * where the instance may have a point to the dynamic
544 * space placed into it (e.g. the cache-name slot), but
545 * the lists and arrays at the time of a purify can be
546 * moved to the RO space. */
548 lispobj result, *new, *old;
550 nwords = CEILING(1 + HeaderValue(header), 2);
553 old = (lispobj *)native_pointer(thing);
554 new = newspace_alloc(nwords, 0); /* inconstant */
557 bcopy(old, new, nwords * sizeof(lispobj));
559 /* Deposit forwarding pointer. */
560 result = make_lispobj(new, lowtag_of(thing));
564 pscav(new, nwords, 1);
570 return NIL; /* dummy value: return something ... */
575 ptrans_fdefn(lispobj thing, lispobj header)
578 lispobj result, *new, *old, oldfn;
581 nwords = CEILING(1 + HeaderValue(header), 2);
584 old = (lispobj *)native_pointer(thing);
585 new = newspace_alloc(nwords, 0); /* inconstant */
588 bcopy(old, new, nwords * sizeof(lispobj));
590 /* Deposit forwarding pointer. */
591 result = make_lispobj(new, lowtag_of(thing));
594 /* Scavenge the function. */
595 fdefn = (struct fdefn *)new;
597 pscav(&fdefn->fun, 1, 0);
598 if ((char *)oldfn + FUN_RAW_ADDR_OFFSET == fdefn->raw_addr)
599 fdefn->raw_addr = (char *)fdefn->fun + FUN_RAW_ADDR_OFFSET;
605 ptrans_unboxed(lispobj thing, lispobj header)
608 lispobj result, *new, *old;
610 nwords = CEILING(1 + HeaderValue(header), 2);
613 old = (lispobj *)native_pointer(thing);
614 new = newspace_alloc(nwords,1); /* always constant */
617 bcopy(old, new, nwords * sizeof(lispobj));
619 /* Deposit forwarding pointer. */
620 result = make_lispobj(new , lowtag_of(thing));
627 ptrans_vector(lispobj thing, long bits, long extra,
628 boolean boxed, boolean constant)
630 struct vector *vector;
632 lispobj result, *new;
635 vector = (struct vector *)native_pointer(thing);
636 length = fixnum_value(vector->length)+extra;
637 // Argh, handle simple-vector-nil separately.
641 nwords = CEILING(NWORDS(length, bits) + 2, 2);
644 new=newspace_alloc(nwords, (constant || !boxed));
645 bcopy(vector, new, nwords * sizeof(lispobj));
647 result = make_lispobj(new, lowtag_of(thing));
648 vector->header = result;
651 pscav(new, nwords, constant);
656 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
658 apply_code_fixups_during_purify(struct code *old_code, struct code *new_code)
660 long nheader_words, ncode_words, nwords;
661 void *constants_start_addr, *constants_end_addr;
662 void *code_start_addr, *code_end_addr;
663 lispobj fixups = NIL;
664 unsigned displacement = (unsigned)new_code - (unsigned)old_code;
665 struct vector *fixups_vector;
667 ncode_words = fixnum_value(new_code->code_size);
668 nheader_words = HeaderValue(*(lispobj *)new_code);
669 nwords = ncode_words + nheader_words;
671 constants_start_addr = (void *)new_code + 5 * N_WORD_BYTES;
672 constants_end_addr = (void *)new_code + nheader_words*N_WORD_BYTES;
673 code_start_addr = (void *)new_code + nheader_words*N_WORD_BYTES;
674 code_end_addr = (void *)new_code + nwords*N_WORD_BYTES;
676 /* The first constant should be a pointer to the fixups for this
677 * code objects. Check. */
678 fixups = new_code->constants[0];
680 /* It will be 0 or the unbound-marker if there are no fixups, and
681 * will be an other-pointer to a vector if it is valid. */
683 (fixups==UNBOUND_MARKER_WIDETAG) ||
684 !is_lisp_pointer(fixups)) {
685 #ifdef LISP_FEATURE_GENCGC
686 /* Check for a possible errors. */
687 sniff_code_object(new_code,displacement);
692 fixups_vector = (struct vector *)native_pointer(fixups);
694 /* Could be pointing to a forwarding pointer. */
695 if (is_lisp_pointer(fixups) && (dynamic_pointer_p(fixups))
696 && forwarding_pointer_p(*(lispobj *)fixups_vector)) {
697 /* If so then follow it. */
699 (struct vector *)native_pointer(*(lispobj *)fixups_vector);
702 if (widetag_of(fixups_vector->header) == SIMPLE_ARRAY_WORD_WIDETAG) {
703 /* We got the fixups for the code block. Now work through the
704 * vector, and apply a fixup at each address. */
705 long length = fixnum_value(fixups_vector->length);
707 for (i=0; i<length; i++) {
708 unsigned offset = fixups_vector->data[i];
709 /* Now check the current value of offset. */
711 *(unsigned *)((unsigned)code_start_addr + offset);
713 /* If it's within the old_code object then it must be an
714 * absolute fixup (relative ones are not saved) */
715 if ((old_value>=(unsigned)old_code)
716 && (old_value<((unsigned)old_code + nwords * N_WORD_BYTES)))
717 /* So add the dispacement. */
718 *(unsigned *)((unsigned)code_start_addr + offset) = old_value
721 /* It is outside the old code object so it must be a relative
722 * fixup (absolute fixups are not saved). So subtract the
724 *(unsigned *)((unsigned)code_start_addr + offset) = old_value
729 /* No longer need the fixups. */
730 new_code->constants[0] = 0;
732 #ifdef LISP_FEATURE_GENCGC
733 /* Check for possible errors. */
734 sniff_code_object(new_code,displacement);
740 ptrans_code(lispobj thing)
742 struct code *code, *new;
744 lispobj func, result;
746 code = (struct code *)native_pointer(thing);
747 nwords = CEILING(HeaderValue(code->header) + fixnum_value(code->code_size),
750 new = (struct code *)newspace_alloc(nwords,1); /* constant */
752 bcopy(code, new, nwords * sizeof(lispobj));
754 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
755 apply_code_fixups_during_purify(code,new);
758 result = make_lispobj(new, OTHER_POINTER_LOWTAG);
760 /* Stick in a forwarding pointer for the code object. */
761 *(lispobj *)code = result;
763 /* Put in forwarding pointers for all the functions. */
764 for (func = code->entry_points;
766 func = ((struct simple_fun *)native_pointer(func))->next) {
768 gc_assert(lowtag_of(func) == FUN_POINTER_LOWTAG);
770 *(lispobj *)native_pointer(func) = result + (func - thing);
773 /* Arrange to scavenge the debug info later. */
774 pscav_later(&new->debug_info, 1);
776 /* FIXME: why would this be a fixnum? */
777 /* "why" is a hard word, but apparently for compiled functions the
778 trace_table_offset contains the length of the instructions, as
779 a fixnum. See CODE-INST-AREA-LENGTH in
780 src/compiler/target-disassem.lisp. -- CSR, 2004-01-08 */
781 if (!(fixnump(new->trace_table_offset)))
783 pscav(&new->trace_table_offset, 1, 0);
785 new->trace_table_offset = NIL; /* limit lifetime */
788 /* Scavenge the constants. */
789 pscav(new->constants, HeaderValue(new->header)-5, 1);
791 /* Scavenge all the functions. */
792 pscav(&new->entry_points, 1, 1);
793 for (func = new->entry_points;
795 func = ((struct simple_fun *)native_pointer(func))->next) {
796 gc_assert(lowtag_of(func) == FUN_POINTER_LOWTAG);
797 gc_assert(!dynamic_pointer_p(func));
799 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
800 /* Temporarily convert the self pointer to a real function pointer. */
801 ((struct simple_fun *)native_pointer(func))->self
802 -= FUN_RAW_ADDR_OFFSET;
804 pscav(&((struct simple_fun *)native_pointer(func))->self, 2, 1);
805 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
806 ((struct simple_fun *)native_pointer(func))->self
807 += FUN_RAW_ADDR_OFFSET;
809 pscav_later(&((struct simple_fun *)native_pointer(func))->name, 3);
816 ptrans_func(lispobj thing, lispobj header)
819 lispobj code, *new, *old, result;
820 struct simple_fun *function;
822 /* Thing can either be a function header, a closure function
823 * header, a closure, or a funcallable-instance. If it's a closure
824 * or a funcallable-instance, we do the same as ptrans_boxed.
825 * Otherwise we have to do something strange, 'cause it is buried
826 * inside a code object. */
828 if (widetag_of(header) == SIMPLE_FUN_HEADER_WIDETAG) {
830 /* We can only end up here if the code object has not been
831 * scavenged, because if it had been scavenged, forwarding pointers
832 * would have been left behind for all the entry points. */
834 function = (struct simple_fun *)native_pointer(thing);
837 ((native_pointer(thing) -
838 (HeaderValue(function->header))), OTHER_POINTER_LOWTAG);
840 /* This will cause the function's header to be replaced with a
841 * forwarding pointer. */
845 /* So we can just return that. */
846 return function->header;
849 /* It's some kind of closure-like thing. */
850 nwords = CEILING(1 + HeaderValue(header), 2);
851 old = (lispobj *)native_pointer(thing);
853 /* Allocate the new one. FINs *must* not go in read_only
854 * space. Closures can; they never change */
857 (nwords,(widetag_of(header)!=FUNCALLABLE_INSTANCE_HEADER_WIDETAG));
860 bcopy(old, new, nwords * sizeof(lispobj));
862 /* Deposit forwarding pointer. */
863 result = make_lispobj(new, lowtag_of(thing));
867 pscav(new, nwords, 0);
874 ptrans_returnpc(lispobj thing, lispobj header)
878 /* Find the corresponding code object. */
879 code = thing - HeaderValue(header)*sizeof(lispobj);
881 /* Make sure it's been transported. */
882 new = *(lispobj *)native_pointer(code);
883 if (!forwarding_pointer_p(new))
884 new = ptrans_code(code);
886 /* Maintain the offset: */
887 return new + (thing - code);
890 #define WORDS_PER_CONS CEILING(sizeof(struct cons) / sizeof(lispobj), 2)
893 ptrans_list(lispobj thing, boolean constant)
895 struct cons *old, *new, *orig;
898 orig = (struct cons *) newspace_alloc(0,constant);
902 /* Allocate a new cons cell. */
903 old = (struct cons *)native_pointer(thing);
904 new = (struct cons *) newspace_alloc(WORDS_PER_CONS,constant);
906 /* Copy the cons cell and keep a pointer to the cdr. */
908 thing = new->cdr = old->cdr;
910 /* Set up the forwarding pointer. */
911 *(lispobj *)old = make_lispobj(new, LIST_POINTER_LOWTAG);
913 /* And count this cell. */
915 } while (lowtag_of(thing) == LIST_POINTER_LOWTAG &&
916 dynamic_pointer_p(thing) &&
917 !(forwarding_pointer_p(*(lispobj *)native_pointer(thing))));
919 /* Scavenge the list we just copied. */
920 pscav((lispobj *)orig, length * WORDS_PER_CONS, constant);
922 return make_lispobj(orig, LIST_POINTER_LOWTAG);
926 ptrans_otherptr(lispobj thing, lispobj header, boolean constant)
928 switch (widetag_of(header)) {
929 /* FIXME: this needs a reindent */
931 case SINGLE_FLOAT_WIDETAG:
932 case DOUBLE_FLOAT_WIDETAG:
933 #ifdef LONG_FLOAT_WIDETAG
934 case LONG_FLOAT_WIDETAG:
936 #ifdef COMPLEX_SINGLE_FLOAT_WIDETAG
937 case COMPLEX_SINGLE_FLOAT_WIDETAG:
939 #ifdef COMPLEX_DOUBLE_FLOAT_WIDETAG
940 case COMPLEX_DOUBLE_FLOAT_WIDETAG:
942 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
943 case COMPLEX_LONG_FLOAT_WIDETAG:
946 return ptrans_unboxed(thing, header);
949 case COMPLEX_WIDETAG:
950 case SIMPLE_ARRAY_WIDETAG:
951 case COMPLEX_BASE_STRING_WIDETAG:
952 #ifdef COMPLEX_CHARACTER_STRING_WIDETAG
953 case COMPLEX_CHARACTER_STRING_WIDETAG:
955 case COMPLEX_BIT_VECTOR_WIDETAG:
956 case COMPLEX_VECTOR_NIL_WIDETAG:
957 case COMPLEX_VECTOR_WIDETAG:
958 case COMPLEX_ARRAY_WIDETAG:
959 return ptrans_boxed(thing, header, constant);
961 case VALUE_CELL_HEADER_WIDETAG:
962 case WEAK_POINTER_WIDETAG:
963 return ptrans_boxed(thing, header, 0);
965 case SYMBOL_HEADER_WIDETAG:
966 return ptrans_boxed(thing, header, 0);
968 case SIMPLE_ARRAY_NIL_WIDETAG:
969 return ptrans_vector(thing, 0, 0, 0, constant);
971 case SIMPLE_BASE_STRING_WIDETAG:
972 return ptrans_vector(thing, 8, 1, 0, constant);
974 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
975 case SIMPLE_CHARACTER_STRING_WIDETAG:
976 return ptrans_vector(thing, 32, 1, 0, constant);
979 case SIMPLE_BIT_VECTOR_WIDETAG:
980 return ptrans_vector(thing, 1, 0, 0, constant);
982 case SIMPLE_VECTOR_WIDETAG:
983 return ptrans_vector(thing, N_WORD_BITS, 0, 1, constant);
985 case SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG:
986 return ptrans_vector(thing, 2, 0, 0, constant);
988 case SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG:
989 return ptrans_vector(thing, 4, 0, 0, constant);
991 case SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG:
992 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
993 case SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG:
994 case SIMPLE_ARRAY_UNSIGNED_BYTE_7_WIDETAG:
996 return ptrans_vector(thing, 8, 0, 0, constant);
998 case SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG:
999 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
1000 case SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG:
1001 case SIMPLE_ARRAY_UNSIGNED_BYTE_15_WIDETAG:
1003 return ptrans_vector(thing, 16, 0, 0, constant);
1005 case SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG:
1006 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG
1007 case SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG:
1008 case SIMPLE_ARRAY_UNSIGNED_BYTE_29_WIDETAG:
1010 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
1011 case SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG:
1012 case SIMPLE_ARRAY_UNSIGNED_BYTE_31_WIDETAG:
1014 return ptrans_vector(thing, 32, 0, 0, constant);
1016 #if N_WORD_BITS == 64
1017 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_60_WIDETAG
1018 case SIMPLE_ARRAY_UNSIGNED_BYTE_60_WIDETAG:
1020 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG
1021 case SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG:
1023 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG
1024 case SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG:
1026 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_61_WIDETAG
1027 case SIMPLE_ARRAY_SIGNED_BYTE_61_WIDETAG:
1029 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG
1030 case SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG:
1032 return ptrans_vector(thing, 64, 0, 0, constant);
1035 case SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG:
1036 return ptrans_vector(thing, 32, 0, 0, constant);
1038 case SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG:
1039 return ptrans_vector(thing, 64, 0, 0, constant);
1041 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
1042 case SIMPLE_ARRAY_LONG_FLOAT_WIDETAG:
1043 #ifdef LISP_FEATURE_X86
1044 return ptrans_vector(thing, 96, 0, 0, constant);
1047 return ptrans_vector(thing, 128, 0, 0, constant);
1051 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
1052 case SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG:
1053 return ptrans_vector(thing, 64, 0, 0, constant);
1056 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
1057 case SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG:
1058 return ptrans_vector(thing, 128, 0, 0, constant);
1061 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
1062 case SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG:
1063 #ifdef LISP_FEATURE_X86
1064 return ptrans_vector(thing, 192, 0, 0, constant);
1067 return ptrans_vector(thing, 256, 0, 0, constant);
1071 case CODE_HEADER_WIDETAG:
1072 return ptrans_code(thing);
1074 case RETURN_PC_HEADER_WIDETAG:
1075 return ptrans_returnpc(thing, header);
1078 return ptrans_fdefn(thing, header);
1081 fprintf(stderr, "Invalid widetag: %d\n", widetag_of(header));
1082 /* Should only come across other pointers to the above stuff. */
1089 pscav_fdefn(struct fdefn *fdefn)
1093 fix_func = ((char *)(fdefn->fun+FUN_RAW_ADDR_OFFSET) == fdefn->raw_addr);
1094 pscav(&fdefn->name, 1, 1);
1095 pscav(&fdefn->fun, 1, 0);
1097 fdefn->raw_addr = (char *)(fdefn->fun + FUN_RAW_ADDR_OFFSET);
1098 return sizeof(struct fdefn) / sizeof(lispobj);
1101 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
1102 /* now putting code objects in static space */
1104 pscav_code(struct code*code)
1108 nwords = CEILING(HeaderValue(code->header) + fixnum_value(code->code_size),
1111 /* Arrange to scavenge the debug info later. */
1112 pscav_later(&code->debug_info, 1);
1114 /* Scavenge the constants. */
1115 pscav(code->constants, HeaderValue(code->header)-5, 1);
1117 /* Scavenge all the functions. */
1118 pscav(&code->entry_points, 1, 1);
1119 for (func = code->entry_points;
1121 func = ((struct simple_fun *)native_pointer(func))->next) {
1122 gc_assert(lowtag_of(func) == FUN_POINTER_LOWTAG);
1123 gc_assert(!dynamic_pointer_p(func));
1125 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
1126 /* Temporarily convert the self pointer to a real function
1128 ((struct simple_fun *)native_pointer(func))->self
1129 -= FUN_RAW_ADDR_OFFSET;
1131 pscav(&((struct simple_fun *)native_pointer(func))->self, 2, 1);
1132 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
1133 ((struct simple_fun *)native_pointer(func))->self
1134 += FUN_RAW_ADDR_OFFSET;
1136 pscav_later(&((struct simple_fun *)native_pointer(func))->name, 3);
1139 return CEILING(nwords,2);
1144 pscav(lispobj *addr, long nwords, boolean constant)
1146 lispobj thing, *thingp, header;
1147 long count = 0; /* (0 = dummy init value to stop GCC warning) */
1148 struct vector *vector;
1150 while (nwords > 0) {
1152 if (is_lisp_pointer(thing)) {
1153 /* It's a pointer. Is it something we might have to move? */
1154 if (dynamic_pointer_p(thing)) {
1155 /* Maybe. Have we already moved it? */
1156 thingp = (lispobj *)native_pointer(thing);
1158 if (is_lisp_pointer(header) && forwarding_pointer_p(header))
1159 /* Yep, so just copy the forwarding pointer. */
1162 /* Nope, copy the object. */
1163 switch (lowtag_of(thing)) {
1164 case FUN_POINTER_LOWTAG:
1165 thing = ptrans_func(thing, header);
1168 case LIST_POINTER_LOWTAG:
1169 thing = ptrans_list(thing, constant);
1172 case INSTANCE_POINTER_LOWTAG:
1173 thing = ptrans_instance(thing, header, constant);
1176 case OTHER_POINTER_LOWTAG:
1177 thing = ptrans_otherptr(thing, header, constant);
1181 /* It was a pointer, but not one of them? */
1189 else if (thing & FIXNUM_TAG_MASK) {
1190 /* It's an other immediate. Maybe the header for an unboxed */
1192 switch (widetag_of(thing)) {
1193 case BIGNUM_WIDETAG:
1194 case SINGLE_FLOAT_WIDETAG:
1195 case DOUBLE_FLOAT_WIDETAG:
1196 #ifdef LONG_FLOAT_WIDETAG
1197 case LONG_FLOAT_WIDETAG:
1200 /* It's an unboxed simple object. */
1201 count = CEILING(HeaderValue(thing)+1, 2);
1204 case SIMPLE_VECTOR_WIDETAG:
1205 if (HeaderValue(thing) == subtype_VectorValidHashing) {
1206 *addr = (subtype_VectorMustRehash << N_WIDETAG_BITS) |
1207 SIMPLE_VECTOR_WIDETAG;
1212 case SIMPLE_ARRAY_NIL_WIDETAG:
1216 case SIMPLE_BASE_STRING_WIDETAG:
1217 vector = (struct vector *)addr;
1218 count = CEILING(NWORDS(fixnum_value(vector->length)+1,8)+2,2);
1221 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
1222 case SIMPLE_CHARACTER_STRING_WIDETAG:
1223 vector = (struct vector *)addr;
1224 count = CEILING(NWORDS(fixnum_value(vector->length)+1,32)+2,2);
1228 case SIMPLE_BIT_VECTOR_WIDETAG:
1229 vector = (struct vector *)addr;
1230 count = CEILING(NWORDS(fixnum_value(vector->length),1)+2,2);
1233 case SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG:
1234 vector = (struct vector *)addr;
1235 count = CEILING(NWORDS(fixnum_value(vector->length),2)+2,2);
1238 case SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG:
1239 vector = (struct vector *)addr;
1240 count = CEILING(NWORDS(fixnum_value(vector->length),4)+2,2);
1243 case SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG:
1244 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
1245 case SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG:
1246 case SIMPLE_ARRAY_UNSIGNED_BYTE_7_WIDETAG:
1248 vector = (struct vector *)addr;
1249 count = CEILING(NWORDS(fixnum_value(vector->length),8)+2,2);
1252 case SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG:
1253 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
1254 case SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG:
1255 case SIMPLE_ARRAY_UNSIGNED_BYTE_15_WIDETAG:
1257 vector = (struct vector *)addr;
1258 count = CEILING(NWORDS(fixnum_value(vector->length),16)+2,2);
1261 case SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG:
1262 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG
1263 case SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG:
1264 case SIMPLE_ARRAY_UNSIGNED_BYTE_29_WIDETAG:
1266 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
1267 case SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG:
1268 case SIMPLE_ARRAY_UNSIGNED_BYTE_31_WIDETAG:
1270 vector = (struct vector *)addr;
1271 count = CEILING(NWORDS(fixnum_value(vector->length),32)+2,2);
1274 #if N_WORD_BITS == 64
1275 case SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG:
1276 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_61_WIDETAG
1277 case SIMPLE_ARRAY_SIGNED_BYTE_61_WIDETAG:
1278 case SIMPLE_ARRAY_UNSIGNED_BYTE_60_WIDETAG:
1280 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG
1281 case SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG:
1282 case SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG:
1284 vector = (struct vector *)addr;
1285 count = CEILING(NWORDS(fixnum_value(vector->length),64)+2,2);
1289 case SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG:
1290 vector = (struct vector *)addr;
1291 count = CEILING(NWORDS(fixnum_value(vector->length), 32) + 2,
1295 case SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG:
1296 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
1297 case SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG:
1299 vector = (struct vector *)addr;
1300 count = CEILING(NWORDS(fixnum_value(vector->length), 64) + 2,
1304 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
1305 case SIMPLE_ARRAY_LONG_FLOAT_WIDETAG:
1306 vector = (struct vector *)addr;
1307 #ifdef LISP_FEATURE_X86
1308 count = fixnum_value(vector->length)*3+2;
1311 count = fixnum_value(vector->length)*4+2;
1316 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
1317 case SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG:
1318 vector = (struct vector *)addr;
1319 count = CEILING(NWORDS(fixnum_value(vector->length), 128) + 2,
1324 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
1325 case SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG:
1326 vector = (struct vector *)addr;
1327 #ifdef LISP_FEATURE_X86
1328 count = fixnum_value(vector->length)*6+2;
1331 count = fixnum_value(vector->length)*8+2;
1336 case CODE_HEADER_WIDETAG:
1337 #if !defined(LISP_FEATURE_X86) && !defined(LISP_FEATURE_X86_64)
1338 gc_abort(); /* no code headers in static space */
1340 count = pscav_code((struct code*)addr);
1344 case SIMPLE_FUN_HEADER_WIDETAG:
1345 case RETURN_PC_HEADER_WIDETAG:
1346 /* We should never hit any of these, 'cause they occur
1347 * buried in the middle of code objects. */
1351 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
1352 case CLOSURE_HEADER_WIDETAG:
1353 case FUNCALLABLE_INSTANCE_HEADER_WIDETAG:
1354 /* The function self pointer needs special care on the
1355 * x86 because it is the real entry point. */
1357 lispobj fun = ((struct closure *)addr)->fun
1358 - FUN_RAW_ADDR_OFFSET;
1359 pscav(&fun, 1, constant);
1360 ((struct closure *)addr)->fun = fun + FUN_RAW_ADDR_OFFSET;
1366 case WEAK_POINTER_WIDETAG:
1367 /* Weak pointers get preserved during purify, 'cause I
1368 * don't feel like figuring out how to break them. */
1369 pscav(addr+1, 2, constant);
1374 /* We have to handle fdefn objects specially, so we
1375 * can fix up the raw function address. */
1376 count = pscav_fdefn((struct fdefn *)addr);
1385 /* It's a fixnum. */
1397 purify(lispobj static_roots, lispobj read_only_roots)
1401 struct later *laters, *next;
1402 struct thread *thread;
1404 if(all_threads->next) {
1405 /* FIXME: there should be _some_ sensible error reporting
1406 * convention. See following comment too */
1407 fprintf(stderr,"Can't purify when more than one thread exists\n");
1413 printf("[doing purification:");
1416 #ifdef LISP_FEATURE_GENCGC
1417 gc_alloc_update_all_page_tables();
1419 for_each_thread(thread)
1420 if (fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX,thread)) != 0) {
1421 /* FIXME: 1. What does this mean? 2. It shouldn't be reporting
1422 * its error simply by a. printing a string b. to stdout instead
1424 printf(" Ack! Can't purify interrupt contexts. ");
1429 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
1430 dynamic_space_free_pointer =
1431 (lispobj*)SymbolValue(ALLOCATION_POINTER,0);
1434 read_only_end = read_only_free =
1435 (lispobj *)SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0);
1436 static_end = static_free =
1437 (lispobj *)SymbolValue(STATIC_SPACE_FREE_POINTER,0);
1444 #if defined(LISP_FEATURE_GENCGC) && (defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64))
1445 /* note this expects only one thread to be active. We'd have to
1446 * stop all the others in the same way as GC does if we wanted
1447 * PURIFY to work when >1 thread exists */
1448 setup_i386_stack_scav(((&static_roots)-2),
1449 ((void *)all_threads->control_stack_end));
1452 pscav(&static_roots, 1, 0);
1453 pscav(&read_only_roots, 1, 1);
1456 printf(" handlers");
1459 pscav((lispobj *) all_threads->interrupt_data->interrupt_handlers,
1460 sizeof(all_threads->interrupt_data->interrupt_handlers)
1468 #if !defined(LISP_FEATURE_X86) && !defined(LISP_FEATURE_X86_64)
1469 pscav((lispobj *)all_threads->control_stack_start,
1470 current_control_stack_pointer -
1471 all_threads->control_stack_start,
1474 #ifdef LISP_FEATURE_GENCGC
1480 printf(" bindings");
1483 #if !(defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64))
1484 pscav( (lispobj *)all_threads->binding_stack_start,
1485 (lispobj *)current_binding_stack_pointer -
1486 all_threads->binding_stack_start,
1489 for_each_thread(thread) {
1490 pscav( (lispobj *)thread->binding_stack_start,
1491 (lispobj *)SymbolValue(BINDING_STACK_POINTER,thread) -
1492 (lispobj *)thread->binding_stack_start,
1494 pscav( (lispobj *) (thread+1),
1495 fixnum_value(SymbolValue(FREE_TLS_INDEX,0)) -
1496 (sizeof (struct thread))/(sizeof (lispobj)),
1503 /* The original CMU CL code had scavenge-read-only-space code
1504 * controlled by the Lisp-level variable
1505 * *SCAVENGE-READ-ONLY-SPACE*. It was disabled by default, and it
1506 * wasn't documented under what circumstances it was useful or
1507 * safe to turn it on, so it's been turned off in SBCL. If you
1508 * want/need this functionality, and can test and document it,
1509 * please submit a patch. */
1511 if (SymbolValue(SCAVENGE_READ_ONLY_SPACE) != UNBOUND_MARKER_WIDETAG
1512 && SymbolValue(SCAVENGE_READ_ONLY_SPACE) != NIL) {
1513 unsigned read_only_space_size =
1514 (lispobj *)SymbolValue(READ_ONLY_SPACE_FREE_POINTER) -
1515 (lispobj *)READ_ONLY_SPACE_START;
1517 "scavenging read only space: %d bytes\n",
1518 read_only_space_size * sizeof(lispobj));
1519 pscav( (lispobj *)READ_ONLY_SPACE_START, read_only_space_size, 0);
1527 clean = (lispobj *)STATIC_SPACE_START;
1529 while (clean != static_free)
1530 clean = pscav(clean, static_free - clean, 0);
1531 laters = later_blocks;
1532 count = later_count;
1533 later_blocks = NULL;
1535 while (laters != NULL) {
1536 for (i = 0; i < count; i++) {
1537 if (laters->u[i].count == 0) {
1539 } else if (laters->u[i].count <= LATERMAXCOUNT) {
1540 pscav(laters->u[i+1].ptr, laters->u[i].count, 1);
1543 pscav(laters->u[i].ptr, 1, 1);
1546 next = laters->next;
1549 count = LATERBLOCKSIZE;
1551 } while (clean != static_free || later_blocks != NULL);
1558 os_zero((os_vm_address_t) current_dynamic_space,
1559 (os_vm_size_t) DYNAMIC_SPACE_SIZE);
1561 /* Zero the stack. Note that the stack is also zeroed by SUB-GC
1562 * calling SCRUB-CONTROL-STACK - this zeros the stack on the x86. */
1563 #if !defined(LISP_FEATURE_X86) && !defined(LISP_FEATURE_X86_64)
1564 os_zero((os_vm_address_t) current_control_stack_pointer,
1566 ((all_threads->control_stack_end -
1567 current_control_stack_pointer) * sizeof(lispobj)));
1570 /* It helps to update the heap free pointers so that free_heap can
1571 * verify after it's done. */
1572 SetSymbolValue(READ_ONLY_SPACE_FREE_POINTER, (lispobj)read_only_free,0);
1573 SetSymbolValue(STATIC_SPACE_FREE_POINTER, (lispobj)static_free,0);
1575 #if !defined(ALLOCATION_POINTER)
1576 dynamic_space_free_pointer = current_dynamic_space;
1577 set_auto_gc_trigger(bytes_consed_between_gcs);
1579 #if defined LISP_FEATURE_GENCGC
1582 #error unsupported case /* in CMU CL, was "ibmrt using GC" */