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>
25 #include "interrupt.h"
35 /* again, what's so special about the x86 that this is differently
36 * visible there than on other platforms? -dan 20010125
38 static lispobj *dynamic_space_free_pointer;
42 lose("GC invariant lost, file \"%s\", line %d", __FILE__, __LINE__)
45 #define gc_assert(ex) do { \
46 if (!(ex)) gc_abort(); \
53 /* These hold the original end of the read_only and static spaces so
54 * we can tell what are forwarding pointers. */
56 static lispobj *read_only_end, *static_end;
58 static lispobj *read_only_free, *static_free;
60 static lispobj *pscav(lispobj *addr, int nwords, boolean constant);
62 #define LATERBLOCKSIZE 1020
63 #define LATERMAXCOUNT 10
72 } *later_blocks = NULL;
73 static int later_count = 0;
75 #define CEILING(x,y) (((x) + ((y) - 1)) & (~((y) - 1)))
76 #define NWORDS(x,y) (CEILING((x),(y)) / (y))
78 /* FIXME: (1) Shouldn't this be defined in sbcl.h? */
80 #define FUN_RAW_ADDR_OFFSET 0
82 #define FUN_RAW_ADDR_OFFSET (6*sizeof(lispobj) - FUN_POINTER_LOWTAG)
86 forwarding_pointer_p(lispobj obj)
92 return ((static_end <= ptr && ptr <= static_free) ||
93 (read_only_end <= ptr && ptr <= read_only_free));
97 dynamic_pointer_p(lispobj ptr)
100 /* KLUDGE: This has an implicit dependence on the ordering of
101 * address spaces, and is therefore basically wrong. I'd fix it,
102 * but I don't have a non-386 port to test it on. Porters are
103 * encouraged to fix it. -- WHN 2000-10-17 */
104 return (ptr >= (lispobj)DYNAMIC_SPACE_START);
106 /* Be more conservative, and remember, this is a maybe. */
107 return (ptr >= (lispobj)DYNAMIC_SPACE_START
109 ptr < (lispobj)dynamic_space_free_pointer);
118 * enhanced x86/GENCGC stack scavenging by Douglas Crosher
120 * Scavenging the stack on the i386 is problematic due to conservative
121 * roots and raw return addresses. Here it is handled in two passes:
122 * the first pass runs before any objects are moved and tries to
123 * identify valid pointers and return address on the stack, the second
124 * pass scavenges these.
127 static unsigned pointer_filter_verbose = 0;
129 /* FIXME: This is substantially the same code as in gencgc.c. (There
130 * are some differences, at least (1) the gencgc.c code needs to worry
131 * about return addresses on the stack pinning code objects, (2) the
132 * gencgc.c code needs to worry about the GC maybe happening in an
133 * interrupt service routine when the main thread of control was
134 * interrupted just as it had allocated memory and before it
135 * initialized it, while PURIFY needn't worry about that, and (3) the
136 * gencgc.c code has mutated more under maintenance since the fork
137 * from CMU CL than the code here has.) The two versions should be
138 * made to explicitly share common code, instead of just two different
139 * cut-and-pasted versions. */
141 valid_dynamic_space_pointer(lispobj *pointer, lispobj *start_addr)
143 /* If it's not a return address then it needs to be a valid Lisp
145 if (!is_lisp_pointer((lispobj)pointer))
148 /* Check that the object pointed to is consistent with the pointer
150 switch (lowtagof((lispobj)pointer)) {
151 case FUN_POINTER_LOWTAG:
152 /* Start_addr should be the enclosing code object, or a closure
154 switch (TypeOf(*start_addr)) {
155 case type_CodeHeader:
156 /* This case is probably caught above. */
158 case type_ClosureHeader:
159 case type_FuncallableInstanceHeader:
160 if ((int)pointer != ((int)start_addr+FUN_POINTER_LOWTAG)) {
161 if (pointer_filter_verbose) {
162 fprintf(stderr,"*Wf2: %x %x %x\n", (unsigned int) pointer,
163 (unsigned int) start_addr, *start_addr);
169 if (pointer_filter_verbose) {
170 fprintf(stderr,"*Wf3: %x %x %x\n", (unsigned int) pointer,
171 (unsigned int) start_addr, *start_addr);
176 case LIST_POINTER_LOWTAG:
177 if ((int)pointer != ((int)start_addr+LIST_POINTER_LOWTAG)) {
178 if (pointer_filter_verbose)
179 fprintf(stderr,"*Wl1: %x %x %x\n", (unsigned int) pointer,
180 (unsigned int) start_addr, *start_addr);
183 /* Is it plausible cons? */
184 if((is_lisp_pointer(start_addr[0])
185 || ((start_addr[0] & 3) == 0) /* fixnum */
186 || (TypeOf(start_addr[0]) == type_BaseChar)
187 || (TypeOf(start_addr[0]) == type_UnboundMarker))
188 && (is_lisp_pointer(start_addr[1])
189 || ((start_addr[1] & 3) == 0) /* fixnum */
190 || (TypeOf(start_addr[1]) == type_BaseChar)
191 || (TypeOf(start_addr[1]) == type_UnboundMarker))) {
194 if (pointer_filter_verbose) {
195 fprintf(stderr,"*Wl2: %x %x %x\n", (unsigned int) pointer,
196 (unsigned int) start_addr, *start_addr);
200 case INSTANCE_POINTER_LOWTAG:
201 if ((int)pointer != ((int)start_addr+INSTANCE_POINTER_LOWTAG)) {
202 if (pointer_filter_verbose) {
203 fprintf(stderr,"*Wi1: %x %x %x\n", (unsigned int) pointer,
204 (unsigned int) start_addr, *start_addr);
208 if (TypeOf(start_addr[0]) != type_InstanceHeader) {
209 if (pointer_filter_verbose) {
210 fprintf(stderr,"*Wi2: %x %x %x\n", (unsigned int) pointer,
211 (unsigned int) start_addr, *start_addr);
216 case OTHER_POINTER_LOWTAG:
217 if ((int)pointer != ((int)start_addr+OTHER_POINTER_LOWTAG)) {
218 if (pointer_filter_verbose) {
219 fprintf(stderr,"*Wo1: %x %x %x\n", (unsigned int) pointer,
220 (unsigned int) start_addr, *start_addr);
224 /* Is it plausible? Not a cons. X should check the headers. */
225 if(is_lisp_pointer(start_addr[0]) || ((start_addr[0] & 3) == 0)) {
226 if (pointer_filter_verbose) {
227 fprintf(stderr,"*Wo2: %x %x %x\n", (unsigned int) pointer,
228 (unsigned int) start_addr, *start_addr);
232 switch (TypeOf(start_addr[0])) {
233 case type_UnboundMarker:
235 if (pointer_filter_verbose) {
236 fprintf(stderr,"*Wo3: %x %x %x\n", (unsigned int) pointer,
237 (unsigned int) start_addr, *start_addr);
241 /* only pointed to by function pointers? */
242 case type_ClosureHeader:
243 case type_FuncallableInstanceHeader:
244 if (pointer_filter_verbose) {
245 fprintf(stderr,"*Wo4: %x %x %x\n", (unsigned int) pointer,
246 (unsigned int) start_addr, *start_addr);
250 case type_InstanceHeader:
251 if (pointer_filter_verbose) {
252 fprintf(stderr,"*Wo5: %x %x %x\n", (unsigned int) pointer,
253 (unsigned int) start_addr, *start_addr);
257 /* the valid other immediate pointer objects */
258 case type_SimpleVector:
261 #ifdef type_ComplexSingleFloat
262 case type_ComplexSingleFloat:
264 #ifdef type_ComplexDoubleFloat
265 case type_ComplexDoubleFloat:
267 #ifdef type_ComplexLongFloat
268 case type_ComplexLongFloat:
270 case type_SimpleArray:
271 case type_ComplexString:
272 case type_ComplexBitVector:
273 case type_ComplexVector:
274 case type_ComplexArray:
275 case type_ValueCellHeader:
276 case type_SymbolHeader:
278 case type_CodeHeader:
280 case type_SingleFloat:
281 case type_DoubleFloat:
282 #ifdef type_LongFloat
285 case type_SimpleString:
286 case type_SimpleBitVector:
287 case type_SimpleArrayUnsignedByte2:
288 case type_SimpleArrayUnsignedByte4:
289 case type_SimpleArrayUnsignedByte8:
290 case type_SimpleArrayUnsignedByte16:
291 case type_SimpleArrayUnsignedByte32:
292 #ifdef type_SimpleArraySignedByte8
293 case type_SimpleArraySignedByte8:
295 #ifdef type_SimpleArraySignedByte16
296 case type_SimpleArraySignedByte16:
298 #ifdef type_SimpleArraySignedByte30
299 case type_SimpleArraySignedByte30:
301 #ifdef type_SimpleArraySignedByte32
302 case type_SimpleArraySignedByte32:
304 case type_SimpleArraySingleFloat:
305 case type_SimpleArrayDoubleFloat:
306 #ifdef type_SimpleArrayLongFloat
307 case type_SimpleArrayLongFloat:
309 #ifdef type_SimpleArrayComplexSingleFloat
310 case type_SimpleArrayComplexSingleFloat:
312 #ifdef type_SimpleArrayComplexDoubleFloat
313 case type_SimpleArrayComplexDoubleFloat:
315 #ifdef type_SimpleArrayComplexLongFloat
316 case type_SimpleArrayComplexLongFloat:
319 case type_WeakPointer:
323 if (pointer_filter_verbose) {
324 fprintf(stderr,"*Wo6: %x %x %x\n", (unsigned int) pointer,
325 (unsigned int) start_addr, *start_addr);
331 if (pointer_filter_verbose) {
332 fprintf(stderr,"*W?: %x %x %x\n", (unsigned int) pointer,
333 (unsigned int) start_addr, *start_addr);
342 #define MAX_STACK_POINTERS 256
343 lispobj *valid_stack_locations[MAX_STACK_POINTERS];
344 unsigned int num_valid_stack_locations;
346 #define MAX_STACK_RETURN_ADDRESSES 128
347 lispobj *valid_stack_ra_locations[MAX_STACK_RETURN_ADDRESSES];
348 lispobj *valid_stack_ra_code_objects[MAX_STACK_RETURN_ADDRESSES];
349 unsigned int num_valid_stack_ra_locations;
351 /* Identify valid stack slots. */
353 setup_i386_stack_scav(lispobj *lowaddr, lispobj *base)
355 lispobj *sp = lowaddr;
356 num_valid_stack_locations = 0;
357 num_valid_stack_ra_locations = 0;
358 for (sp = lowaddr; sp < base; sp++) {
360 /* Find the object start address */
361 lispobj *start_addr = search_dynamic_space((void *)thing);
363 /* We need to allow raw pointers into Code objects for
364 * return addresses. This will also pick up pointers to
365 * functions in code objects. */
366 if (TypeOf(*start_addr) == type_CodeHeader) {
367 gc_assert(num_valid_stack_ra_locations <
368 MAX_STACK_RETURN_ADDRESSES);
369 valid_stack_ra_locations[num_valid_stack_ra_locations] = sp;
370 valid_stack_ra_code_objects[num_valid_stack_ra_locations++] =
371 (lispobj *)((int)start_addr + OTHER_POINTER_LOWTAG);
373 if (valid_dynamic_space_pointer((void *)thing, start_addr)) {
374 gc_assert(num_valid_stack_locations < MAX_STACK_POINTERS);
375 valid_stack_locations[num_valid_stack_locations++] = sp;
380 if (pointer_filter_verbose) {
381 fprintf(stderr, "number of valid stack pointers = %d\n",
382 num_valid_stack_locations);
383 fprintf(stderr, "number of stack return addresses = %d\n",
384 num_valid_stack_ra_locations);
389 pscav_i386_stack(void)
393 for (i = 0; i < num_valid_stack_locations; i++)
394 pscav(valid_stack_locations[i], 1, 0);
396 for (i = 0; i < num_valid_stack_ra_locations; i++) {
397 lispobj code_obj = (lispobj)valid_stack_ra_code_objects[i];
398 pscav(&code_obj, 1, 0);
399 if (pointer_filter_verbose) {
400 fprintf(stderr,"*C moved RA %x to %x; for code object %x to %x\n",
401 *valid_stack_ra_locations[i],
402 (int)(*valid_stack_ra_locations[i])
403 - ((int)valid_stack_ra_code_objects[i] - (int)code_obj),
404 (unsigned int) valid_stack_ra_code_objects[i], code_obj);
406 *valid_stack_ra_locations[i] =
407 ((int)(*valid_stack_ra_locations[i])
408 - ((int)valid_stack_ra_code_objects[i] - (int)code_obj));
416 pscav_later(lispobj *where, int count)
420 if (count > LATERMAXCOUNT) {
421 while (count > LATERMAXCOUNT) {
422 pscav_later(where, LATERMAXCOUNT);
423 count -= LATERMAXCOUNT;
424 where += LATERMAXCOUNT;
428 if (later_blocks == NULL || later_count == LATERBLOCKSIZE ||
429 (later_count == LATERBLOCKSIZE-1 && count > 1)) {
430 new = (struct later *)malloc(sizeof(struct later));
431 new->next = later_blocks;
432 if (later_blocks && later_count < LATERBLOCKSIZE)
433 later_blocks->u[later_count].ptr = NULL;
439 later_blocks->u[later_count++].count = count;
440 later_blocks->u[later_count++].ptr = where;
445 ptrans_boxed(lispobj thing, lispobj header, boolean constant)
448 lispobj result, *new, *old;
450 nwords = 1 + HeaderValue(header);
453 old = (lispobj *)native_pointer(thing);
455 new = read_only_free;
456 read_only_free += CEILING(nwords, 2);
460 static_free += CEILING(nwords, 2);
464 bcopy(old, new, nwords * sizeof(lispobj));
466 /* Deposit forwarding pointer. */
467 result = (lispobj)new | lowtagof(thing);
471 pscav(new, nwords, constant);
476 /* We need to look at the layout to see whether it is a pure structure
477 * class, and only then can we transport as constant. If it is pure,
478 * we can ALWAYS transport as a constant. */
480 ptrans_instance(lispobj thing, lispobj header, boolean constant)
482 lispobj layout = ((struct instance *)native_pointer(thing))->slots[0];
483 lispobj pure = ((struct instance *)native_pointer(layout))->slots[15];
487 return (ptrans_boxed(thing, header, 1));
489 return (ptrans_boxed(thing, header, 0));
492 /* Substructure: special case for the COMPACT-INFO-ENVs,
493 * where the instance may have a point to the dynamic
494 * space placed into it (e.g. the cache-name slot), but
495 * the lists and arrays at the time of a purify can be
496 * moved to the RO space. */
498 lispobj result, *new, *old;
500 nwords = 1 + HeaderValue(header);
503 old = (lispobj *)native_pointer(thing);
505 static_free += CEILING(nwords, 2);
508 bcopy(old, new, nwords * sizeof(lispobj));
510 /* Deposit forwarding pointer. */
511 result = (lispobj)new | lowtagof(thing);
515 pscav(new, nwords, 1);
521 return NIL; /* dummy value: return something ... */
526 ptrans_fdefn(lispobj thing, lispobj header)
529 lispobj result, *new, *old, oldfn;
532 nwords = 1 + HeaderValue(header);
535 old = (lispobj *)native_pointer(thing);
537 static_free += CEILING(nwords, 2);
540 bcopy(old, new, nwords * sizeof(lispobj));
542 /* Deposit forwarding pointer. */
543 result = (lispobj)new | lowtagof(thing);
546 /* Scavenge the function. */
547 fdefn = (struct fdefn *)new;
549 pscav(&fdefn->fun, 1, 0);
550 if ((char *)oldfn + FUN_RAW_ADDR_OFFSET == fdefn->raw_addr)
551 fdefn->raw_addr = (char *)fdefn->fun + FUN_RAW_ADDR_OFFSET;
557 ptrans_unboxed(lispobj thing, lispobj header)
560 lispobj result, *new, *old;
562 nwords = 1 + HeaderValue(header);
565 old = (lispobj *)native_pointer(thing);
566 new = read_only_free;
567 read_only_free += CEILING(nwords, 2);
570 bcopy(old, new, nwords * sizeof(lispobj));
572 /* Deposit forwarding pointer. */
573 result = (lispobj)new | lowtagof(thing);
580 ptrans_vector(lispobj thing, int bits, int extra,
581 boolean boxed, boolean constant)
583 struct vector *vector;
585 lispobj result, *new;
587 vector = (struct vector *)native_pointer(thing);
588 nwords = 2 + (CEILING((fixnum_value(vector->length)+extra)*bits,32)>>5);
590 if (boxed && !constant) {
592 static_free += CEILING(nwords, 2);
595 new = read_only_free;
596 read_only_free += CEILING(nwords, 2);
599 bcopy(vector, new, nwords * sizeof(lispobj));
601 result = (lispobj)new | lowtagof(thing);
602 vector->header = result;
605 pscav(new, nwords, constant);
612 apply_code_fixups_during_purify(struct code *old_code, struct code *new_code)
614 int nheader_words, ncode_words, nwords;
615 void *constants_start_addr, *constants_end_addr;
616 void *code_start_addr, *code_end_addr;
617 lispobj fixups = NIL;
618 unsigned displacement = (unsigned)new_code - (unsigned)old_code;
619 struct vector *fixups_vector;
621 ncode_words = fixnum_value(new_code->code_size);
622 nheader_words = HeaderValue(*(lispobj *)new_code);
623 nwords = ncode_words + nheader_words;
625 constants_start_addr = (void *)new_code + 5*4;
626 constants_end_addr = (void *)new_code + nheader_words*4;
627 code_start_addr = (void *)new_code + nheader_words*4;
628 code_end_addr = (void *)new_code + nwords*4;
630 /* The first constant should be a pointer to the fixups for this
631 * code objects. Check. */
632 fixups = new_code->constants[0];
634 /* It will be 0 or the unbound-marker if there are no fixups, and
635 * will be an other-pointer to a vector if it is valid. */
637 (fixups==type_UnboundMarker) ||
638 !is_lisp_pointer(fixups)) {
640 /* Check for a possible errors. */
641 sniff_code_object(new_code,displacement);
646 fixups_vector = (struct vector *)native_pointer(fixups);
648 /* Could be pointing to a forwarding pointer. */
649 if (is_lisp_pointer(fixups) && (dynamic_pointer_p(fixups))
650 && forwarding_pointer_p(*(lispobj *)fixups_vector)) {
651 /* If so then follow it. */
652 fixups_vector = (struct vector *)native_pointer(*(lispobj *)fixups_vector);
655 if (TypeOf(fixups_vector->header) == type_SimpleArrayUnsignedByte32) {
656 /* We got the fixups for the code block. Now work through the vector,
657 * and apply a fixup at each address. */
658 int length = fixnum_value(fixups_vector->length);
660 for (i=0; i<length; i++) {
661 unsigned offset = fixups_vector->data[i];
662 /* Now check the current value of offset. */
663 unsigned old_value = *(unsigned *)((unsigned)code_start_addr + offset);
665 /* If it's within the old_code object then it must be an
666 * absolute fixup (relative ones are not saved) */
667 if ((old_value>=(unsigned)old_code)
668 && (old_value<((unsigned)old_code + nwords*4)))
669 /* So add the dispacement. */
670 *(unsigned *)((unsigned)code_start_addr + offset) = old_value
673 /* It is outside the old code object so it must be a relative
674 * fixup (absolute fixups are not saved). So subtract the
676 *(unsigned *)((unsigned)code_start_addr + offset) = old_value
681 /* No longer need the fixups. */
682 new_code->constants[0] = 0;
685 /* Check for possible errors. */
686 sniff_code_object(new_code,displacement);
692 ptrans_code(lispobj thing)
694 struct code *code, *new;
696 lispobj func, result;
698 code = (struct code *)native_pointer(thing);
699 nwords = HeaderValue(code->header) + fixnum_value(code->code_size);
701 new = (struct code *)read_only_free;
702 read_only_free += CEILING(nwords, 2);
704 bcopy(code, new, nwords * sizeof(lispobj));
707 apply_code_fixups_during_purify(code,new);
710 result = (lispobj)new | OTHER_POINTER_LOWTAG;
712 /* Stick in a forwarding pointer for the code object. */
713 *(lispobj *)code = result;
715 /* Put in forwarding pointers for all the functions. */
716 for (func = code->entry_points;
718 func = ((struct simple_fun *)native_pointer(func))->next) {
720 gc_assert(lowtagof(func) == FUN_POINTER_LOWTAG);
722 *(lispobj *)native_pointer(func) = result + (func - thing);
725 /* Arrange to scavenge the debug info later. */
726 pscav_later(&new->debug_info, 1);
728 if(new->trace_table_offset & 0x3)
730 pscav(&new->trace_table_offset, 1, 0);
732 new->trace_table_offset = NIL; /* limit lifetime */
735 /* Scavenge the constants. */
736 pscav(new->constants, HeaderValue(new->header)-5, 1);
738 /* Scavenge all the functions. */
739 pscav(&new->entry_points, 1, 1);
740 for (func = new->entry_points;
742 func = ((struct simple_fun *)native_pointer(func))->next) {
743 gc_assert(lowtagof(func) == FUN_POINTER_LOWTAG);
744 gc_assert(!dynamic_pointer_p(func));
747 /* Temporarly convert the self pointer to a real function pointer. */
748 ((struct simple_fun *)native_pointer(func))->self
749 -= FUN_RAW_ADDR_OFFSET;
751 pscav(&((struct simple_fun *)native_pointer(func))->self, 2, 1);
753 ((struct simple_fun *)native_pointer(func))->self
754 += FUN_RAW_ADDR_OFFSET;
756 pscav_later(&((struct simple_fun *)native_pointer(func))->name, 3);
763 ptrans_func(lispobj thing, lispobj header)
766 lispobj code, *new, *old, result;
767 struct simple_fun *function;
769 /* Thing can either be a function header, a closure function
770 * header, a closure, or a funcallable-instance. If it's a closure
771 * or a funcallable-instance, we do the same as ptrans_boxed.
772 * Otherwise we have to do something strange, 'cause it is buried
773 * inside a code object. */
775 if (TypeOf(header) == type_SimpleFunHeader ||
776 TypeOf(header) == type_ClosureFunHeader) {
778 /* We can only end up here if the code object has not been
779 * scavenged, because if it had been scavenged, forwarding pointers
780 * would have been left behind for all the entry points. */
782 function = (struct simple_fun *)native_pointer(thing);
784 (native_pointer(thing) -
785 (HeaderValue(function->header)*sizeof(lispobj))) |
786 OTHER_POINTER_LOWTAG;
788 /* This will cause the function's header to be replaced with a
789 * forwarding pointer. */
792 /* So we can just return that. */
793 return function->header;
796 /* It's some kind of closure-like thing. */
797 nwords = 1 + HeaderValue(header);
798 old = (lispobj *)native_pointer(thing);
800 /* Allocate the new one. */
801 if (TypeOf(header) == type_FuncallableInstanceHeader) {
802 /* FINs *must* not go in read_only space. */
804 static_free += CEILING(nwords, 2);
807 /* Closures can always go in read-only space, 'cause they
810 new = read_only_free;
811 read_only_free += CEILING(nwords, 2);
814 bcopy(old, new, nwords * sizeof(lispobj));
816 /* Deposit forwarding pointer. */
817 result = (lispobj)new | lowtagof(thing);
821 pscav(new, nwords, 0);
828 ptrans_returnpc(lispobj thing, lispobj header)
832 /* Find the corresponding code object. */
833 code = thing - HeaderValue(header)*sizeof(lispobj);
835 /* Make sure it's been transported. */
836 new = *(lispobj *)native_pointer(code);
837 if (!forwarding_pointer_p(new))
838 new = ptrans_code(code);
840 /* Maintain the offset: */
841 return new + (thing - code);
844 #define WORDS_PER_CONS CEILING(sizeof(struct cons) / sizeof(lispobj), 2)
847 ptrans_list(lispobj thing, boolean constant)
849 struct cons *old, *new, *orig;
853 orig = (struct cons *)read_only_free;
855 orig = (struct cons *)static_free;
859 /* Allocate a new cons cell. */
860 old = (struct cons *)native_pointer(thing);
862 new = (struct cons *)read_only_free;
863 read_only_free += WORDS_PER_CONS;
866 new = (struct cons *)static_free;
867 static_free += WORDS_PER_CONS;
870 /* Copy the cons cell and keep a pointer to the cdr. */
872 thing = new->cdr = old->cdr;
874 /* Set up the forwarding pointer. */
875 *(lispobj *)old = ((lispobj)new) | LIST_POINTER_LOWTAG;
877 /* And count this cell. */
879 } while (lowtagof(thing) == LIST_POINTER_LOWTAG &&
880 dynamic_pointer_p(thing) &&
881 !(forwarding_pointer_p(*(lispobj *)native_pointer(thing))));
883 /* Scavenge the list we just copied. */
884 pscav((lispobj *)orig, length * WORDS_PER_CONS, constant);
886 return ((lispobj)orig) | LIST_POINTER_LOWTAG;
890 ptrans_otherptr(lispobj thing, lispobj header, boolean constant)
892 switch (TypeOf(header)) {
894 case type_SingleFloat:
895 case type_DoubleFloat:
896 #ifdef type_LongFloat
899 #ifdef type_ComplexSingleFloat
900 case type_ComplexSingleFloat:
902 #ifdef type_ComplexDoubleFloat
903 case type_ComplexDoubleFloat:
905 #ifdef type_ComplexLongFloat
906 case type_ComplexLongFloat:
909 return ptrans_unboxed(thing, header);
913 case type_SimpleArray:
914 case type_ComplexString:
915 case type_ComplexVector:
916 case type_ComplexArray:
917 return ptrans_boxed(thing, header, constant);
919 case type_ValueCellHeader:
920 case type_WeakPointer:
921 return ptrans_boxed(thing, header, 0);
923 case type_SymbolHeader:
924 return ptrans_boxed(thing, header, 0);
926 case type_SimpleString:
927 return ptrans_vector(thing, 8, 1, 0, constant);
929 case type_SimpleBitVector:
930 return ptrans_vector(thing, 1, 0, 0, constant);
932 case type_SimpleVector:
933 return ptrans_vector(thing, 32, 0, 1, constant);
935 case type_SimpleArrayUnsignedByte2:
936 return ptrans_vector(thing, 2, 0, 0, constant);
938 case type_SimpleArrayUnsignedByte4:
939 return ptrans_vector(thing, 4, 0, 0, constant);
941 case type_SimpleArrayUnsignedByte8:
942 #ifdef type_SimpleArraySignedByte8
943 case type_SimpleArraySignedByte8:
945 return ptrans_vector(thing, 8, 0, 0, constant);
947 case type_SimpleArrayUnsignedByte16:
948 #ifdef type_SimpleArraySignedByte16
949 case type_SimpleArraySignedByte16:
951 return ptrans_vector(thing, 16, 0, 0, constant);
953 case type_SimpleArrayUnsignedByte32:
954 #ifdef type_SimpleArraySignedByte30
955 case type_SimpleArraySignedByte30:
957 #ifdef type_SimpleArraySignedByte32
958 case type_SimpleArraySignedByte32:
960 return ptrans_vector(thing, 32, 0, 0, constant);
962 case type_SimpleArraySingleFloat:
963 return ptrans_vector(thing, 32, 0, 0, constant);
965 case type_SimpleArrayDoubleFloat:
966 return ptrans_vector(thing, 64, 0, 0, constant);
968 #ifdef type_SimpleArrayLongFloat
969 case type_SimpleArrayLongFloat:
971 return ptrans_vector(thing, 96, 0, 0, constant);
974 return ptrans_vector(thing, 128, 0, 0, constant);
978 #ifdef type_SimpleArrayComplexSingleFloat
979 case type_SimpleArrayComplexSingleFloat:
980 return ptrans_vector(thing, 64, 0, 0, constant);
983 #ifdef type_SimpleArrayComplexDoubleFloat
984 case type_SimpleArrayComplexDoubleFloat:
985 return ptrans_vector(thing, 128, 0, 0, constant);
988 #ifdef type_SimpleArrayComplexLongFloat
989 case type_SimpleArrayComplexLongFloat:
991 return ptrans_vector(thing, 192, 0, 0, constant);
994 return ptrans_vector(thing, 256, 0, 0, constant);
998 case type_CodeHeader:
999 return ptrans_code(thing);
1001 case type_ReturnPcHeader:
1002 return ptrans_returnpc(thing, header);
1005 return ptrans_fdefn(thing, header);
1008 /* Should only come across other pointers to the above stuff. */
1015 pscav_fdefn(struct fdefn *fdefn)
1019 fix_func = ((char *)(fdefn->fun+FUN_RAW_ADDR_OFFSET) == fdefn->raw_addr);
1020 pscav(&fdefn->name, 1, 1);
1021 pscav(&fdefn->fun, 1, 0);
1023 fdefn->raw_addr = (char *)(fdefn->fun + FUN_RAW_ADDR_OFFSET);
1024 return sizeof(struct fdefn) / sizeof(lispobj);
1028 /* now putting code objects in static space */
1030 pscav_code(struct code*code)
1034 nwords = HeaderValue(code->header) + fixnum_value(code->code_size);
1036 /* Arrange to scavenge the debug info later. */
1037 pscav_later(&code->debug_info, 1);
1039 /* Scavenge the constants. */
1040 pscav(code->constants, HeaderValue(code->header)-5, 1);
1042 /* Scavenge all the functions. */
1043 pscav(&code->entry_points, 1, 1);
1044 for (func = code->entry_points;
1046 func = ((struct simple_fun *)native_pointer(func))->next) {
1047 gc_assert(lowtagof(func) == FUN_POINTER_LOWTAG);
1048 gc_assert(!dynamic_pointer_p(func));
1051 /* Temporarly convert the self pointer to a real function
1053 ((struct simple_fun *)native_pointer(func))->self
1054 -= FUN_RAW_ADDR_OFFSET;
1056 pscav(&((struct simple_fun *)native_pointer(func))->self, 2, 1);
1058 ((struct simple_fun *)native_pointer(func))->self
1059 += FUN_RAW_ADDR_OFFSET;
1061 pscav_later(&((struct simple_fun *)native_pointer(func))->name, 3);
1064 return CEILING(nwords,2);
1069 pscav(lispobj *addr, int nwords, boolean constant)
1071 lispobj thing, *thingp, header;
1072 int count = 0; /* (0 = dummy init value to stop GCC warning) */
1073 struct vector *vector;
1075 while (nwords > 0) {
1077 if (is_lisp_pointer(thing)) {
1078 /* It's a pointer. Is it something we might have to move? */
1079 if (dynamic_pointer_p(thing)) {
1080 /* Maybe. Have we already moved it? */
1081 thingp = (lispobj *)native_pointer(thing);
1083 if (is_lisp_pointer(header) && forwarding_pointer_p(header))
1084 /* Yep, so just copy the forwarding pointer. */
1087 /* Nope, copy the object. */
1088 switch (lowtagof(thing)) {
1089 case FUN_POINTER_LOWTAG:
1090 thing = ptrans_func(thing, header);
1093 case LIST_POINTER_LOWTAG:
1094 thing = ptrans_list(thing, constant);
1097 case INSTANCE_POINTER_LOWTAG:
1098 thing = ptrans_instance(thing, header, constant);
1101 case OTHER_POINTER_LOWTAG:
1102 thing = ptrans_otherptr(thing, header, constant);
1106 /* It was a pointer, but not one of them? */
1114 else if (thing & 3) {
1115 /* It's an other immediate. Maybe the header for an unboxed */
1117 switch (TypeOf(thing)) {
1119 case type_SingleFloat:
1120 case type_DoubleFloat:
1121 #ifdef type_LongFloat
1122 case type_LongFloat:
1125 /* It's an unboxed simple object. */
1126 count = HeaderValue(thing)+1;
1129 case type_SimpleVector:
1130 if (HeaderValue(thing) == subtype_VectorValidHashing)
1131 *addr = (subtype_VectorMustRehash<<N_TYPE_BITS) |
1136 case type_SimpleString:
1137 vector = (struct vector *)addr;
1138 count = CEILING(NWORDS(fixnum_value(vector->length)+1,4)+2,2);
1141 case type_SimpleBitVector:
1142 vector = (struct vector *)addr;
1143 count = CEILING(NWORDS(fixnum_value(vector->length),32)+2,2);
1146 case type_SimpleArrayUnsignedByte2:
1147 vector = (struct vector *)addr;
1148 count = CEILING(NWORDS(fixnum_value(vector->length),16)+2,2);
1151 case type_SimpleArrayUnsignedByte4:
1152 vector = (struct vector *)addr;
1153 count = CEILING(NWORDS(fixnum_value(vector->length),8)+2,2);
1156 case type_SimpleArrayUnsignedByte8:
1157 #ifdef type_SimpleArraySignedByte8
1158 case type_SimpleArraySignedByte8:
1160 vector = (struct vector *)addr;
1161 count = CEILING(NWORDS(fixnum_value(vector->length),4)+2,2);
1164 case type_SimpleArrayUnsignedByte16:
1165 #ifdef type_SimpleArraySignedByte16
1166 case type_SimpleArraySignedByte16:
1168 vector = (struct vector *)addr;
1169 count = CEILING(NWORDS(fixnum_value(vector->length),2)+2,2);
1172 case type_SimpleArrayUnsignedByte32:
1173 #ifdef type_SimpleArraySignedByte30
1174 case type_SimpleArraySignedByte30:
1176 #ifdef type_SimpleArraySignedByte32
1177 case type_SimpleArraySignedByte32:
1179 vector = (struct vector *)addr;
1180 count = CEILING(fixnum_value(vector->length)+2,2);
1183 case type_SimpleArraySingleFloat:
1184 vector = (struct vector *)addr;
1185 count = CEILING(fixnum_value(vector->length)+2,2);
1188 case type_SimpleArrayDoubleFloat:
1189 #ifdef type_SimpleArrayComplexSingleFloat
1190 case type_SimpleArrayComplexSingleFloat:
1192 vector = (struct vector *)addr;
1193 count = fixnum_value(vector->length)*2+2;
1196 #ifdef type_SimpleArrayLongFloat
1197 case type_SimpleArrayLongFloat:
1198 vector = (struct vector *)addr;
1200 count = fixnum_value(vector->length)*3+2;
1203 count = fixnum_value(vector->length)*4+2;
1208 #ifdef type_SimpleArrayComplexDoubleFloat
1209 case type_SimpleArrayComplexDoubleFloat:
1210 vector = (struct vector *)addr;
1211 count = fixnum_value(vector->length)*4+2;
1215 #ifdef type_SimpleArrayComplexLongFloat
1216 case type_SimpleArrayComplexLongFloat:
1217 vector = (struct vector *)addr;
1219 count = fixnum_value(vector->length)*6+2;
1222 count = fixnum_value(vector->length)*8+2;
1227 case type_CodeHeader:
1229 gc_abort(); /* no code headers in static space */
1231 count = pscav_code((struct code*)addr);
1235 case type_SimpleFunHeader:
1236 case type_ClosureFunHeader:
1237 case type_ReturnPcHeader:
1238 /* We should never hit any of these, 'cause they occur
1239 * buried in the middle of code objects. */
1244 case type_ClosureHeader:
1245 case type_FuncallableInstanceHeader:
1246 /* The function self pointer needs special care on the
1247 * x86 because it is the real entry point. */
1249 lispobj fun = ((struct closure *)addr)->fun
1250 - FUN_RAW_ADDR_OFFSET;
1251 pscav(&fun, 1, constant);
1252 ((struct closure *)addr)->fun = fun + FUN_RAW_ADDR_OFFSET;
1258 case type_WeakPointer:
1259 /* Weak pointers get preserved during purify, 'cause I
1260 * don't feel like figuring out how to break them. */
1261 pscav(addr+1, 2, constant);
1266 /* We have to handle fdefn objects specially, so we
1267 * can fix up the raw function address. */
1268 count = pscav_fdefn((struct fdefn *)addr);
1277 /* It's a fixnum. */
1289 purify(lispobj static_roots, lispobj read_only_roots)
1293 struct later *laters, *next;
1296 printf("[doing purification:");
1300 if (fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX)) != 0) {
1301 /* FIXME: 1. What does this mean? 2. It shouldn't be reporting
1302 * its error simply by a. printing a string b. to stdout instead
1304 printf(" Ack! Can't purify interrupt contexts. ");
1309 #if defined(__i386__)
1310 dynamic_space_free_pointer =
1311 (lispobj*)SymbolValue(ALLOCATION_POINTER);
1314 read_only_end = read_only_free =
1315 (lispobj *)SymbolValue(READ_ONLY_SPACE_FREE_POINTER);
1316 static_end = static_free =
1317 (lispobj *)SymbolValue(STATIC_SPACE_FREE_POINTER);
1325 gc_assert((lispobj *)CONTROL_STACK_END > ((&read_only_roots)+1));
1326 setup_i386_stack_scav(((&static_roots)-2), (lispobj *)CONTROL_STACK_END);
1329 pscav(&static_roots, 1, 0);
1330 pscav(&read_only_roots, 1, 1);
1333 printf(" handlers");
1336 pscav((lispobj *) interrupt_handlers,
1337 sizeof(interrupt_handlers) / sizeof(lispobj),
1345 pscav((lispobj *)CONTROL_STACK_START,
1346 current_control_stack_pointer - (lispobj *)CONTROL_STACK_START,
1355 printf(" bindings");
1358 #if !defined(__i386__)
1359 pscav( (lispobj *)BINDING_STACK_START,
1360 (lispobj *)current_binding_stack_pointer - (lispobj *)BINDING_STACK_START,
1363 pscav( (lispobj *)BINDING_STACK_START,
1364 (lispobj *)SymbolValue(BINDING_STACK_POINTER) -
1365 (lispobj *)BINDING_STACK_START,
1369 /* The original CMU CL code had scavenge-read-only-space code
1370 * controlled by the Lisp-level variable
1371 * *SCAVENGE-READ-ONLY-SPACE*. It was disabled by default, and it
1372 * wasn't documented under what circumstances it was useful or
1373 * safe to turn it on, so it's been turned off in SBCL. If you
1374 * want/need this functionality, and can test and document it,
1375 * please submit a patch. */
1377 if (SymbolValue(SCAVENGE_READ_ONLY_SPACE) != type_UnboundMarker
1378 && SymbolValue(SCAVENGE_READ_ONLY_SPACE) != NIL) {
1379 unsigned read_only_space_size =
1380 (lispobj *)SymbolValue(READ_ONLY_SPACE_FREE_POINTER) -
1381 (lispobj *)READ_ONLY_SPACE_START;
1383 "scavenging read only space: %d bytes\n",
1384 read_only_space_size * sizeof(lispobj));
1385 pscav( (lispobj *)READ_ONLY_SPACE_START, read_only_space_size, 0);
1393 clean = (lispobj *)STATIC_SPACE_START;
1395 while (clean != static_free)
1396 clean = pscav(clean, static_free - clean, 0);
1397 laters = later_blocks;
1398 count = later_count;
1399 later_blocks = NULL;
1401 while (laters != NULL) {
1402 for (i = 0; i < count; i++) {
1403 if (laters->u[i].count == 0) {
1405 } else if (laters->u[i].count <= LATERMAXCOUNT) {
1406 pscav(laters->u[i+1].ptr, laters->u[i].count, 1);
1409 pscav(laters->u[i].ptr, 1, 1);
1412 next = laters->next;
1415 count = LATERBLOCKSIZE;
1417 } while (clean != static_free || later_blocks != NULL);
1424 os_zero((os_vm_address_t) current_dynamic_space,
1425 (os_vm_size_t) DYNAMIC_SPACE_SIZE);
1427 /* Zero the stack. Note that the stack is also zeroed by SUB-GC
1428 * calling SCRUB-CONTROL-STACK - this zeros the stack on the x86. */
1430 os_zero((os_vm_address_t) current_control_stack_pointer,
1431 (os_vm_size_t) (CONTROL_STACK_SIZE -
1432 ((current_control_stack_pointer -
1433 (lispobj *)CONTROL_STACK_START) *
1437 /* It helps to update the heap free pointers so that free_heap can
1438 * verify after it's done. */
1439 SetSymbolValue(READ_ONLY_SPACE_FREE_POINTER, (lispobj)read_only_free);
1440 SetSymbolValue(STATIC_SPACE_FREE_POINTER, (lispobj)static_free);
1442 #if !defined(__i386__)
1443 dynamic_space_free_pointer = current_dynamic_space;
1448 #error unsupported case /* in CMU CL, was "ibmrt using GC" */