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))
79 #define RAW_ADDR_OFFSET 0
81 #define RAW_ADDR_OFFSET (6*sizeof(lispobj) - type_FunctionPointer)
85 forwarding_pointer_p(lispobj obj)
91 return ((static_end <= ptr && ptr <= static_free) ||
92 (read_only_end <= ptr && ptr <= read_only_free));
96 dynamic_pointer_p(lispobj ptr)
99 /* KLUDGE: This has an implicit dependence on the ordering of
100 * address spaces, and is therefore basically wrong. I'd fix it,
101 * but I don't have a non-386 port to test it on. Porters are
102 * encouraged to fix it. -- WHN 2000-10-17 */
103 return (ptr >= (lispobj)DYNAMIC_SPACE_START);
105 /* Be more conservative, and remember, this is a maybe. */
106 return (ptr >= (lispobj)DYNAMIC_SPACE_START
108 ptr < (lispobj)dynamic_space_free_pointer);
117 * enhanced x86/GENCGC stack scavenging by Douglas Crosher
119 * Scavenging the stack on the i386 is problematic due to conservative
120 * roots and raw return addresses. Here it is handled in two passes:
121 * the first pass runs before any objects are moved and tries to
122 * identify valid pointers and return address on the stack, the second
123 * pass scavenges these.
126 static unsigned pointer_filter_verbose = 0;
128 /* FIXME: This is substantially the same code as in gencgc.c. (There
129 * are some differences, at least (1) the gencgc.c code needs to worry
130 * about return addresses on the stack pinning code objects, (2) the
131 * gencgc.c code needs to worry about the GC maybe happening in an
132 * interrupt service routine when the main thread of control was
133 * interrupted just as it had allocated memory and before it
134 * initialized it, while PURIFY needn't worry about that, and (3) the
135 * gencgc.c code has mutated more under maintenance since the fork
136 * from CMU CL than the code here has.) The two versions should be
137 * made to explicitly share common code, instead of just two different
138 * cut-and-pasted versions. */
140 valid_dynamic_space_pointer(lispobj *pointer, lispobj *start_addr)
142 /* If it's not a return address then it needs to be a valid Lisp
144 if (!is_lisp_pointer((lispobj)pointer))
147 /* Check that the object pointed to is consistent with the pointer
149 switch (LowtagOf((lispobj)pointer)) {
150 case type_FunctionPointer:
151 /* Start_addr should be the enclosing code object, or a closure
153 switch (TypeOf(*start_addr)) {
154 case type_CodeHeader:
155 /* This case is probably caught above. */
157 case type_ClosureHeader:
158 case type_FuncallableInstanceHeader:
159 if ((int)pointer != ((int)start_addr+type_FunctionPointer)) {
160 if (pointer_filter_verbose) {
161 fprintf(stderr,"*Wf2: %x %x %x\n", (unsigned int) pointer,
162 (unsigned int) start_addr, *start_addr);
168 if (pointer_filter_verbose) {
169 fprintf(stderr,"*Wf3: %x %x %x\n", (unsigned int) pointer,
170 (unsigned int) start_addr, *start_addr);
175 case type_ListPointer:
176 if ((int)pointer != ((int)start_addr+type_ListPointer)) {
177 if (pointer_filter_verbose)
178 fprintf(stderr,"*Wl1: %x %x %x\n", (unsigned int) pointer,
179 (unsigned int) start_addr, *start_addr);
182 /* Is it plausible cons? */
183 if((is_lisp_pointer(start_addr[0])
184 || ((start_addr[0] & 3) == 0) /* fixnum */
185 || (TypeOf(start_addr[0]) == type_BaseChar)
186 || (TypeOf(start_addr[0]) == type_UnboundMarker))
187 && (is_lisp_pointer(start_addr[1])
188 || ((start_addr[1] & 3) == 0) /* fixnum */
189 || (TypeOf(start_addr[1]) == type_BaseChar)
190 || (TypeOf(start_addr[1]) == type_UnboundMarker))) {
193 if (pointer_filter_verbose) {
194 fprintf(stderr,"*Wl2: %x %x %x\n", (unsigned int) pointer,
195 (unsigned int) start_addr, *start_addr);
199 case type_InstancePointer:
200 if ((int)pointer != ((int)start_addr+type_InstancePointer)) {
201 if (pointer_filter_verbose) {
202 fprintf(stderr,"*Wi1: %x %x %x\n", (unsigned int) pointer,
203 (unsigned int) start_addr, *start_addr);
207 if (TypeOf(start_addr[0]) != type_InstanceHeader) {
208 if (pointer_filter_verbose) {
209 fprintf(stderr,"*Wi2: %x %x %x\n", (unsigned int) pointer,
210 (unsigned int) start_addr, *start_addr);
215 case type_OtherPointer:
216 if ((int)pointer != ((int)start_addr+type_OtherPointer)) {
217 if (pointer_filter_verbose) {
218 fprintf(stderr,"*Wo1: %x %x %x\n", (unsigned int) pointer,
219 (unsigned int) start_addr, *start_addr);
223 /* Is it plausible? Not a cons. X should check the headers. */
224 if(is_lisp_pointer(start_addr[0]) || ((start_addr[0] & 3) == 0)) {
225 if (pointer_filter_verbose) {
226 fprintf(stderr,"*Wo2: %x %x %x\n", (unsigned int) pointer,
227 (unsigned int) start_addr, *start_addr);
231 switch (TypeOf(start_addr[0])) {
232 case type_UnboundMarker:
234 if (pointer_filter_verbose) {
235 fprintf(stderr,"*Wo3: %x %x %x\n", (unsigned int) pointer,
236 (unsigned int) start_addr, *start_addr);
240 /* only pointed to by function pointers? */
241 case type_ClosureHeader:
242 case type_FuncallableInstanceHeader:
243 if (pointer_filter_verbose) {
244 fprintf(stderr,"*Wo4: %x %x %x\n", (unsigned int) pointer,
245 (unsigned int) start_addr, *start_addr);
249 case type_InstanceHeader:
250 if (pointer_filter_verbose) {
251 fprintf(stderr,"*Wo5: %x %x %x\n", (unsigned int) pointer,
252 (unsigned int) start_addr, *start_addr);
256 /* the valid other immediate pointer objects */
257 case type_SimpleVector:
260 #ifdef type_ComplexSingleFloat
261 case type_ComplexSingleFloat:
263 #ifdef type_ComplexDoubleFloat
264 case type_ComplexDoubleFloat:
266 #ifdef type_ComplexLongFloat
267 case type_ComplexLongFloat:
269 case type_SimpleArray:
270 case type_ComplexString:
271 case type_ComplexBitVector:
272 case type_ComplexVector:
273 case type_ComplexArray:
274 case type_ValueCellHeader:
275 case type_SymbolHeader:
277 case type_CodeHeader:
279 case type_SingleFloat:
280 case type_DoubleFloat:
281 #ifdef type_LongFloat
284 case type_SimpleString:
285 case type_SimpleBitVector:
286 case type_SimpleArrayUnsignedByte2:
287 case type_SimpleArrayUnsignedByte4:
288 case type_SimpleArrayUnsignedByte8:
289 case type_SimpleArrayUnsignedByte16:
290 case type_SimpleArrayUnsignedByte32:
291 #ifdef type_SimpleArraySignedByte8
292 case type_SimpleArraySignedByte8:
294 #ifdef type_SimpleArraySignedByte16
295 case type_SimpleArraySignedByte16:
297 #ifdef type_SimpleArraySignedByte30
298 case type_SimpleArraySignedByte30:
300 #ifdef type_SimpleArraySignedByte32
301 case type_SimpleArraySignedByte32:
303 case type_SimpleArraySingleFloat:
304 case type_SimpleArrayDoubleFloat:
305 #ifdef type_SimpleArrayLongFloat
306 case type_SimpleArrayLongFloat:
308 #ifdef type_SimpleArrayComplexSingleFloat
309 case type_SimpleArrayComplexSingleFloat:
311 #ifdef type_SimpleArrayComplexDoubleFloat
312 case type_SimpleArrayComplexDoubleFloat:
314 #ifdef type_SimpleArrayComplexLongFloat
315 case type_SimpleArrayComplexLongFloat:
318 case type_WeakPointer:
322 if (pointer_filter_verbose) {
323 fprintf(stderr,"*Wo6: %x %x %x\n", (unsigned int) pointer,
324 (unsigned int) start_addr, *start_addr);
330 if (pointer_filter_verbose) {
331 fprintf(stderr,"*W?: %x %x %x\n", (unsigned int) pointer,
332 (unsigned int) start_addr, *start_addr);
341 #define MAX_STACK_POINTERS 256
342 lispobj *valid_stack_locations[MAX_STACK_POINTERS];
343 unsigned int num_valid_stack_locations;
345 #define MAX_STACK_RETURN_ADDRESSES 128
346 lispobj *valid_stack_ra_locations[MAX_STACK_RETURN_ADDRESSES];
347 lispobj *valid_stack_ra_code_objects[MAX_STACK_RETURN_ADDRESSES];
348 unsigned int num_valid_stack_ra_locations;
350 /* Identify valid stack slots. */
352 setup_i386_stack_scav(lispobj *lowaddr, lispobj *base)
354 lispobj *sp = lowaddr;
355 num_valid_stack_locations = 0;
356 num_valid_stack_ra_locations = 0;
357 for (sp = lowaddr; sp < base; sp++) {
359 /* Find the object start address */
360 lispobj *start_addr = search_dynamic_space((void *)thing);
362 /* We need to allow raw pointers into Code objects for
363 * return addresses. This will also pick up pointers to
364 * functions in code objects. */
365 if (TypeOf(*start_addr) == type_CodeHeader) {
366 gc_assert(num_valid_stack_ra_locations <
367 MAX_STACK_RETURN_ADDRESSES);
368 valid_stack_ra_locations[num_valid_stack_ra_locations] = sp;
369 valid_stack_ra_code_objects[num_valid_stack_ra_locations++] =
370 (lispobj *)((int)start_addr + type_OtherPointer);
372 if (valid_dynamic_space_pointer((void *)thing, start_addr)) {
373 gc_assert(num_valid_stack_locations < MAX_STACK_POINTERS);
374 valid_stack_locations[num_valid_stack_locations++] = sp;
379 if (pointer_filter_verbose) {
380 fprintf(stderr, "number of valid stack pointers = %d\n",
381 num_valid_stack_locations);
382 fprintf(stderr, "number of stack return addresses = %d\n",
383 num_valid_stack_ra_locations);
388 pscav_i386_stack(void)
392 for (i = 0; i < num_valid_stack_locations; i++)
393 pscav(valid_stack_locations[i], 1, 0);
395 for (i = 0; i < num_valid_stack_ra_locations; i++) {
396 lispobj code_obj = (lispobj)valid_stack_ra_code_objects[i];
397 pscav(&code_obj, 1, 0);
398 if (pointer_filter_verbose) {
399 fprintf(stderr,"*C moved RA %x to %x; for code object %x to %x\n",
400 *valid_stack_ra_locations[i],
401 (int)(*valid_stack_ra_locations[i])
402 - ((int)valid_stack_ra_code_objects[i] - (int)code_obj),
403 (unsigned int) valid_stack_ra_code_objects[i], code_obj);
405 *valid_stack_ra_locations[i] =
406 ((int)(*valid_stack_ra_locations[i])
407 - ((int)valid_stack_ra_code_objects[i] - (int)code_obj));
415 pscav_later(lispobj *where, int count)
419 if (count > LATERMAXCOUNT) {
420 while (count > LATERMAXCOUNT) {
421 pscav_later(where, LATERMAXCOUNT);
422 count -= LATERMAXCOUNT;
423 where += LATERMAXCOUNT;
427 if (later_blocks == NULL || later_count == LATERBLOCKSIZE ||
428 (later_count == LATERBLOCKSIZE-1 && count > 1)) {
429 new = (struct later *)malloc(sizeof(struct later));
430 new->next = later_blocks;
431 if (later_blocks && later_count < LATERBLOCKSIZE)
432 later_blocks->u[later_count].ptr = NULL;
438 later_blocks->u[later_count++].count = count;
439 later_blocks->u[later_count++].ptr = where;
444 ptrans_boxed(lispobj thing, lispobj header, boolean constant)
447 lispobj result, *new, *old;
449 nwords = 1 + HeaderValue(header);
452 old = (lispobj *)native_pointer(thing);
454 new = read_only_free;
455 read_only_free += CEILING(nwords, 2);
459 static_free += CEILING(nwords, 2);
463 bcopy(old, new, nwords * sizeof(lispobj));
465 /* Deposit forwarding pointer. */
466 result = (lispobj)new | LowtagOf(thing);
470 pscav(new, nwords, constant);
475 /* We need to look at the layout to see whether it is a pure structure
476 * class, and only then can we transport as constant. If it is pure,
477 * we can ALWAYS transport as a constant. */
479 ptrans_instance(lispobj thing, lispobj header, boolean constant)
481 lispobj layout = ((struct instance *)native_pointer(thing))->slots[0];
482 lispobj pure = ((struct instance *)native_pointer(layout))->slots[15];
486 return (ptrans_boxed(thing, header, 1));
488 return (ptrans_boxed(thing, header, 0));
491 /* Substructure: special case for the COMPACT-INFO-ENVs,
492 * where the instance may have a point to the dynamic
493 * space placed into it (e.g. the cache-name slot), but
494 * the lists and arrays at the time of a purify can be
495 * moved to the RO space. */
497 lispobj result, *new, *old;
499 nwords = 1 + HeaderValue(header);
502 old = (lispobj *)native_pointer(thing);
504 static_free += CEILING(nwords, 2);
507 bcopy(old, new, nwords * sizeof(lispobj));
509 /* Deposit forwarding pointer. */
510 result = (lispobj)new | LowtagOf(thing);
514 pscav(new, nwords, 1);
520 return NIL; /* dummy value: return something ... */
525 ptrans_fdefn(lispobj thing, lispobj header)
528 lispobj result, *new, *old, oldfn;
531 nwords = 1 + HeaderValue(header);
534 old = (lispobj *)native_pointer(thing);
536 static_free += CEILING(nwords, 2);
539 bcopy(old, new, nwords * sizeof(lispobj));
541 /* Deposit forwarding pointer. */
542 result = (lispobj)new | LowtagOf(thing);
545 /* Scavenge the function. */
546 fdefn = (struct fdefn *)new;
547 oldfn = fdefn->function;
548 pscav(&fdefn->function, 1, 0);
549 if ((char *)oldfn + RAW_ADDR_OFFSET == fdefn->raw_addr)
550 fdefn->raw_addr = (char *)fdefn->function + RAW_ADDR_OFFSET;
556 ptrans_unboxed(lispobj thing, lispobj header)
559 lispobj result, *new, *old;
561 nwords = 1 + HeaderValue(header);
564 old = (lispobj *)native_pointer(thing);
565 new = read_only_free;
566 read_only_free += CEILING(nwords, 2);
569 bcopy(old, new, nwords * sizeof(lispobj));
571 /* Deposit forwarding pointer. */
572 result = (lispobj)new | LowtagOf(thing);
579 ptrans_vector(lispobj thing, int bits, int extra,
580 boolean boxed, boolean constant)
582 struct vector *vector;
584 lispobj result, *new;
586 vector = (struct vector *)native_pointer(thing);
587 nwords = 2 + (CEILING((fixnum_value(vector->length)+extra)*bits,32)>>5);
589 if (boxed && !constant) {
591 static_free += CEILING(nwords, 2);
594 new = read_only_free;
595 read_only_free += CEILING(nwords, 2);
598 bcopy(vector, new, nwords * sizeof(lispobj));
600 result = (lispobj)new | LowtagOf(thing);
601 vector->header = result;
604 pscav(new, nwords, constant);
611 apply_code_fixups_during_purify(struct code *old_code, struct code *new_code)
613 int nheader_words, ncode_words, nwords;
614 void *constants_start_addr, *constants_end_addr;
615 void *code_start_addr, *code_end_addr;
616 lispobj fixups = NIL;
617 unsigned displacement = (unsigned)new_code - (unsigned)old_code;
618 struct vector *fixups_vector;
620 ncode_words = fixnum_value(new_code->code_size);
621 nheader_words = HeaderValue(*(lispobj *)new_code);
622 nwords = ncode_words + nheader_words;
624 constants_start_addr = (void *)new_code + 5*4;
625 constants_end_addr = (void *)new_code + nheader_words*4;
626 code_start_addr = (void *)new_code + nheader_words*4;
627 code_end_addr = (void *)new_code + nwords*4;
629 /* The first constant should be a pointer to the fixups for this
630 * code objects. Check. */
631 fixups = new_code->constants[0];
633 /* It will be 0 or the unbound-marker if there are no fixups, and
634 * will be an other-pointer to a vector if it is valid. */
636 (fixups==type_UnboundMarker) ||
637 !is_lisp_pointer(fixups)) {
639 /* Check for a possible errors. */
640 sniff_code_object(new_code,displacement);
645 fixups_vector = (struct vector *)native_pointer(fixups);
647 /* Could be pointing to a forwarding pointer. */
648 if (is_lisp_pointer(fixups) && (dynamic_pointer_p(fixups))
649 && forwarding_pointer_p(*(lispobj *)fixups_vector)) {
650 /* If so then follow it. */
651 fixups_vector = (struct vector *)native_pointer(*(lispobj *)fixups_vector);
654 if (TypeOf(fixups_vector->header) == type_SimpleArrayUnsignedByte32) {
655 /* We got the fixups for the code block. Now work through the vector,
656 * and apply a fixup at each address. */
657 int length = fixnum_value(fixups_vector->length);
659 for (i=0; i<length; i++) {
660 unsigned offset = fixups_vector->data[i];
661 /* Now check the current value of offset. */
662 unsigned old_value = *(unsigned *)((unsigned)code_start_addr + offset);
664 /* If it's within the old_code object then it must be an
665 * absolute fixup (relative ones are not saved) */
666 if ((old_value>=(unsigned)old_code)
667 && (old_value<((unsigned)old_code + nwords*4)))
668 /* So add the dispacement. */
669 *(unsigned *)((unsigned)code_start_addr + offset) = old_value
672 /* It is outside the old code object so it must be a relative
673 * fixup (absolute fixups are not saved). So subtract the
675 *(unsigned *)((unsigned)code_start_addr + offset) = old_value
680 /* No longer need the fixups. */
681 new_code->constants[0] = 0;
684 /* Check for possible errors. */
685 sniff_code_object(new_code,displacement);
691 ptrans_code(lispobj thing)
693 struct code *code, *new;
695 lispobj func, result;
697 code = (struct code *)native_pointer(thing);
698 nwords = HeaderValue(code->header) + fixnum_value(code->code_size);
700 new = (struct code *)read_only_free;
701 read_only_free += CEILING(nwords, 2);
703 bcopy(code, new, nwords * sizeof(lispobj));
706 apply_code_fixups_during_purify(code,new);
709 result = (lispobj)new | type_OtherPointer;
711 /* Stick in a forwarding pointer for the code object. */
712 *(lispobj *)code = result;
714 /* Put in forwarding pointers for all the functions. */
715 for (func = code->entry_points;
717 func = ((struct function *)native_pointer(func))->next) {
719 gc_assert(LowtagOf(func) == type_FunctionPointer);
721 *(lispobj *)native_pointer(func) = result + (func - thing);
724 /* Arrange to scavenge the debug info later. */
725 pscav_later(&new->debug_info, 1);
727 if(new->trace_table_offset & 0x3)
729 pscav(&new->trace_table_offset, 1, 0);
731 new->trace_table_offset = NIL; /* limit lifetime */
734 /* Scavenge the constants. */
735 pscav(new->constants, HeaderValue(new->header)-5, 1);
737 /* Scavenge all the functions. */
738 pscav(&new->entry_points, 1, 1);
739 for (func = new->entry_points;
741 func = ((struct function *)native_pointer(func))->next) {
742 gc_assert(LowtagOf(func) == type_FunctionPointer);
743 gc_assert(!dynamic_pointer_p(func));
746 /* Temporarly convert the self pointer to a real function
748 ((struct function *)native_pointer(func))->self -= RAW_ADDR_OFFSET;
750 pscav(&((struct function *)native_pointer(func))->self, 2, 1);
752 ((struct function *)native_pointer(func))->self += RAW_ADDR_OFFSET;
754 pscav_later(&((struct function *)native_pointer(func))->name, 3);
761 ptrans_func(lispobj thing, lispobj header)
764 lispobj code, *new, *old, result;
765 struct function *function;
767 /* Thing can either be a function header, a closure function
768 * header, a closure, or a funcallable-instance. If it's a closure
769 * or a funcallable-instance, we do the same as ptrans_boxed.
770 * Otherwise we have to do something strange, 'cause it is buried
771 * inside a code object. */
773 if (TypeOf(header) == type_FunctionHeader ||
774 TypeOf(header) == type_ClosureFunctionHeader) {
776 /* We can only end up here if the code object has not been
777 * scavenged, because if it had been scavenged, forwarding pointers
778 * would have been left behind for all the entry points. */
780 function = (struct function *)native_pointer(thing);
782 (native_pointer(thing) -
783 (HeaderValue(function->header)*sizeof(lispobj))) |
786 /* This will cause the function's header to be replaced with a
787 * forwarding pointer. */
790 /* So we can just return that. */
791 return function->header;
794 /* It's some kind of closure-like thing. */
795 nwords = 1 + HeaderValue(header);
796 old = (lispobj *)native_pointer(thing);
798 /* Allocate the new one. */
799 if (TypeOf(header) == type_FuncallableInstanceHeader) {
800 /* FINs *must* not go in read_only space. */
802 static_free += CEILING(nwords, 2);
805 /* Closures can always go in read-only space, 'cause they
808 new = read_only_free;
809 read_only_free += CEILING(nwords, 2);
812 bcopy(old, new, nwords * sizeof(lispobj));
814 /* Deposit forwarding pointer. */
815 result = (lispobj)new | LowtagOf(thing);
819 pscav(new, nwords, 0);
826 ptrans_returnpc(lispobj thing, lispobj header)
830 /* Find the corresponding code object. */
831 code = thing - HeaderValue(header)*sizeof(lispobj);
833 /* Make sure it's been transported. */
834 new = *(lispobj *)native_pointer(code);
835 if (!forwarding_pointer_p(new))
836 new = ptrans_code(code);
838 /* Maintain the offset: */
839 return new + (thing - code);
842 #define WORDS_PER_CONS CEILING(sizeof(struct cons) / sizeof(lispobj), 2)
845 ptrans_list(lispobj thing, boolean constant)
847 struct cons *old, *new, *orig;
851 orig = (struct cons *)read_only_free;
853 orig = (struct cons *)static_free;
857 /* Allocate a new cons cell. */
858 old = (struct cons *)native_pointer(thing);
860 new = (struct cons *)read_only_free;
861 read_only_free += WORDS_PER_CONS;
864 new = (struct cons *)static_free;
865 static_free += WORDS_PER_CONS;
868 /* Copy the cons cell and keep a pointer to the cdr. */
870 thing = new->cdr = old->cdr;
872 /* Set up the forwarding pointer. */
873 *(lispobj *)old = ((lispobj)new) | type_ListPointer;
875 /* And count this cell. */
877 } while (LowtagOf(thing) == type_ListPointer &&
878 dynamic_pointer_p(thing) &&
879 !(forwarding_pointer_p(*(lispobj *)native_pointer(thing))));
881 /* Scavenge the list we just copied. */
882 pscav((lispobj *)orig, length * WORDS_PER_CONS, constant);
884 return ((lispobj)orig) | type_ListPointer;
888 ptrans_otherptr(lispobj thing, lispobj header, boolean constant)
890 switch (TypeOf(header)) {
892 case type_SingleFloat:
893 case type_DoubleFloat:
894 #ifdef type_LongFloat
897 #ifdef type_ComplexSingleFloat
898 case type_ComplexSingleFloat:
900 #ifdef type_ComplexDoubleFloat
901 case type_ComplexDoubleFloat:
903 #ifdef type_ComplexLongFloat
904 case type_ComplexLongFloat:
907 return ptrans_unboxed(thing, header);
911 case type_SimpleArray:
912 case type_ComplexString:
913 case type_ComplexVector:
914 case type_ComplexArray:
915 return ptrans_boxed(thing, header, constant);
917 case type_ValueCellHeader:
918 case type_WeakPointer:
919 return ptrans_boxed(thing, header, 0);
921 case type_SymbolHeader:
922 return ptrans_boxed(thing, header, 0);
924 case type_SimpleString:
925 return ptrans_vector(thing, 8, 1, 0, constant);
927 case type_SimpleBitVector:
928 return ptrans_vector(thing, 1, 0, 0, constant);
930 case type_SimpleVector:
931 return ptrans_vector(thing, 32, 0, 1, constant);
933 case type_SimpleArrayUnsignedByte2:
934 return ptrans_vector(thing, 2, 0, 0, constant);
936 case type_SimpleArrayUnsignedByte4:
937 return ptrans_vector(thing, 4, 0, 0, constant);
939 case type_SimpleArrayUnsignedByte8:
940 #ifdef type_SimpleArraySignedByte8
941 case type_SimpleArraySignedByte8:
943 return ptrans_vector(thing, 8, 0, 0, constant);
945 case type_SimpleArrayUnsignedByte16:
946 #ifdef type_SimpleArraySignedByte16
947 case type_SimpleArraySignedByte16:
949 return ptrans_vector(thing, 16, 0, 0, constant);
951 case type_SimpleArrayUnsignedByte32:
952 #ifdef type_SimpleArraySignedByte30
953 case type_SimpleArraySignedByte30:
955 #ifdef type_SimpleArraySignedByte32
956 case type_SimpleArraySignedByte32:
958 return ptrans_vector(thing, 32, 0, 0, constant);
960 case type_SimpleArraySingleFloat:
961 return ptrans_vector(thing, 32, 0, 0, constant);
963 case type_SimpleArrayDoubleFloat:
964 return ptrans_vector(thing, 64, 0, 0, constant);
966 #ifdef type_SimpleArrayLongFloat
967 case type_SimpleArrayLongFloat:
969 return ptrans_vector(thing, 96, 0, 0, constant);
972 return ptrans_vector(thing, 128, 0, 0, constant);
976 #ifdef type_SimpleArrayComplexSingleFloat
977 case type_SimpleArrayComplexSingleFloat:
978 return ptrans_vector(thing, 64, 0, 0, constant);
981 #ifdef type_SimpleArrayComplexDoubleFloat
982 case type_SimpleArrayComplexDoubleFloat:
983 return ptrans_vector(thing, 128, 0, 0, constant);
986 #ifdef type_SimpleArrayComplexLongFloat
987 case type_SimpleArrayComplexLongFloat:
989 return ptrans_vector(thing, 192, 0, 0, constant);
992 return ptrans_vector(thing, 256, 0, 0, constant);
996 case type_CodeHeader:
997 return ptrans_code(thing);
999 case type_ReturnPcHeader:
1000 return ptrans_returnpc(thing, header);
1003 return ptrans_fdefn(thing, header);
1006 /* Should only come across other pointers to the above stuff. */
1013 pscav_fdefn(struct fdefn *fdefn)
1017 fix_func = ((char *)(fdefn->function+RAW_ADDR_OFFSET) == fdefn->raw_addr);
1018 pscav(&fdefn->name, 1, 1);
1019 pscav(&fdefn->function, 1, 0);
1021 fdefn->raw_addr = (char *)(fdefn->function + RAW_ADDR_OFFSET);
1022 return sizeof(struct fdefn) / sizeof(lispobj);
1026 /* now putting code objects in static space */
1028 pscav_code(struct code*code)
1032 nwords = HeaderValue(code->header) + fixnum_value(code->code_size);
1034 /* Arrange to scavenge the debug info later. */
1035 pscav_later(&code->debug_info, 1);
1037 /* Scavenge the constants. */
1038 pscav(code->constants, HeaderValue(code->header)-5, 1);
1040 /* Scavenge all the functions. */
1041 pscav(&code->entry_points, 1, 1);
1042 for (func = code->entry_points;
1044 func = ((struct function *)native_pointer(func))->next) {
1045 gc_assert(LowtagOf(func) == type_FunctionPointer);
1046 gc_assert(!dynamic_pointer_p(func));
1049 /* Temporarly convert the self pointer to a real function
1051 ((struct function *)native_pointer(func))->self -= RAW_ADDR_OFFSET;
1053 pscav(&((struct function *)native_pointer(func))->self, 2, 1);
1055 ((struct function *)native_pointer(func))->self += RAW_ADDR_OFFSET;
1057 pscav_later(&((struct function *)native_pointer(func))->name, 3);
1060 return CEILING(nwords,2);
1065 pscav(lispobj *addr, int nwords, boolean constant)
1067 lispobj thing, *thingp, header;
1068 int count = 0; /* (0 = dummy init value to stop GCC warning) */
1069 struct vector *vector;
1071 while (nwords > 0) {
1073 if (is_lisp_pointer(thing)) {
1074 /* It's a pointer. Is it something we might have to move? */
1075 if (dynamic_pointer_p(thing)) {
1076 /* Maybe. Have we already moved it? */
1077 thingp = (lispobj *)native_pointer(thing);
1079 if (is_lisp_pointer(header) && forwarding_pointer_p(header))
1080 /* Yep, so just copy the forwarding pointer. */
1083 /* Nope, copy the object. */
1084 switch (LowtagOf(thing)) {
1085 case type_FunctionPointer:
1086 thing = ptrans_func(thing, header);
1089 case type_ListPointer:
1090 thing = ptrans_list(thing, constant);
1093 case type_InstancePointer:
1094 thing = ptrans_instance(thing, header, constant);
1097 case type_OtherPointer:
1098 thing = ptrans_otherptr(thing, header, constant);
1102 /* It was a pointer, but not one of them? */
1110 else if (thing & 3) {
1111 /* It's an other immediate. Maybe the header for an unboxed */
1113 switch (TypeOf(thing)) {
1115 case type_SingleFloat:
1116 case type_DoubleFloat:
1117 #ifdef type_LongFloat
1118 case type_LongFloat:
1121 /* It's an unboxed simple object. */
1122 count = HeaderValue(thing)+1;
1125 case type_SimpleVector:
1126 if (HeaderValue(thing) == subtype_VectorValidHashing)
1127 *addr = (subtype_VectorMustRehash<<type_Bits) |
1132 case type_SimpleString:
1133 vector = (struct vector *)addr;
1134 count = CEILING(NWORDS(fixnum_value(vector->length)+1,4)+2,2);
1137 case type_SimpleBitVector:
1138 vector = (struct vector *)addr;
1139 count = CEILING(NWORDS(fixnum_value(vector->length),32)+2,2);
1142 case type_SimpleArrayUnsignedByte2:
1143 vector = (struct vector *)addr;
1144 count = CEILING(NWORDS(fixnum_value(vector->length),16)+2,2);
1147 case type_SimpleArrayUnsignedByte4:
1148 vector = (struct vector *)addr;
1149 count = CEILING(NWORDS(fixnum_value(vector->length),8)+2,2);
1152 case type_SimpleArrayUnsignedByte8:
1153 #ifdef type_SimpleArraySignedByte8
1154 case type_SimpleArraySignedByte8:
1156 vector = (struct vector *)addr;
1157 count = CEILING(NWORDS(fixnum_value(vector->length),4)+2,2);
1160 case type_SimpleArrayUnsignedByte16:
1161 #ifdef type_SimpleArraySignedByte16
1162 case type_SimpleArraySignedByte16:
1164 vector = (struct vector *)addr;
1165 count = CEILING(NWORDS(fixnum_value(vector->length),2)+2,2);
1168 case type_SimpleArrayUnsignedByte32:
1169 #ifdef type_SimpleArraySignedByte30
1170 case type_SimpleArraySignedByte30:
1172 #ifdef type_SimpleArraySignedByte32
1173 case type_SimpleArraySignedByte32:
1175 vector = (struct vector *)addr;
1176 count = CEILING(fixnum_value(vector->length)+2,2);
1179 case type_SimpleArraySingleFloat:
1180 vector = (struct vector *)addr;
1181 count = CEILING(fixnum_value(vector->length)+2,2);
1184 case type_SimpleArrayDoubleFloat:
1185 #ifdef type_SimpleArrayComplexSingleFloat
1186 case type_SimpleArrayComplexSingleFloat:
1188 vector = (struct vector *)addr;
1189 count = fixnum_value(vector->length)*2+2;
1192 #ifdef type_SimpleArrayLongFloat
1193 case type_SimpleArrayLongFloat:
1194 vector = (struct vector *)addr;
1196 count = fixnum_value(vector->length)*3+2;
1199 count = fixnum_value(vector->length)*4+2;
1204 #ifdef type_SimpleArrayComplexDoubleFloat
1205 case type_SimpleArrayComplexDoubleFloat:
1206 vector = (struct vector *)addr;
1207 count = fixnum_value(vector->length)*4+2;
1211 #ifdef type_SimpleArrayComplexLongFloat
1212 case type_SimpleArrayComplexLongFloat:
1213 vector = (struct vector *)addr;
1215 count = fixnum_value(vector->length)*6+2;
1218 count = fixnum_value(vector->length)*8+2;
1223 case type_CodeHeader:
1225 gc_abort(); /* no code headers in static space */
1227 count = pscav_code((struct code*)addr);
1231 case type_FunctionHeader:
1232 case type_ClosureFunctionHeader:
1233 case type_ReturnPcHeader:
1234 /* We should never hit any of these, 'cause they occur
1235 * buried in the middle of code objects. */
1240 case type_ClosureHeader:
1241 case type_FuncallableInstanceHeader:
1242 /* The function self pointer needs special care on the
1243 * x86 because it is the real entry point. */
1245 lispobj fun = ((struct closure *)addr)->function
1247 pscav(&fun, 1, constant);
1248 ((struct closure *)addr)->function = fun + RAW_ADDR_OFFSET;
1254 case type_WeakPointer:
1255 /* Weak pointers get preserved during purify, 'cause I
1256 * don't feel like figuring out how to break them. */
1257 pscav(addr+1, 2, constant);
1262 /* We have to handle fdefn objects specially, so we
1263 * can fix up the raw function address. */
1264 count = pscav_fdefn((struct fdefn *)addr);
1273 /* It's a fixnum. */
1285 purify(lispobj static_roots, lispobj read_only_roots)
1289 struct later *laters, *next;
1292 printf("[doing purification:");
1296 if (fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX)) != 0) {
1297 /* FIXME: 1. What does this mean? 2. It shouldn't be reporting
1298 * its error simply by a. printing a string b. to stdout instead
1300 printf(" Ack! Can't purify interrupt contexts. ");
1305 #if defined(__i386__)
1306 dynamic_space_free_pointer =
1307 (lispobj*)SymbolValue(ALLOCATION_POINTER);
1310 read_only_end = read_only_free =
1311 (lispobj *)SymbolValue(READ_ONLY_SPACE_FREE_POINTER);
1312 static_end = static_free =
1313 (lispobj *)SymbolValue(STATIC_SPACE_FREE_POINTER);
1321 gc_assert((lispobj *)CONTROL_STACK_END > ((&read_only_roots)+1));
1322 setup_i386_stack_scav(((&static_roots)-2), (lispobj *)CONTROL_STACK_END);
1325 pscav(&static_roots, 1, 0);
1326 pscav(&read_only_roots, 1, 1);
1329 printf(" handlers");
1332 pscav((lispobj *) interrupt_handlers,
1333 sizeof(interrupt_handlers) / sizeof(lispobj),
1341 pscav((lispobj *)CONTROL_STACK_START,
1342 current_control_stack_pointer - (lispobj *)CONTROL_STACK_START,
1351 printf(" bindings");
1354 #if !defined(__i386__)
1355 pscav( (lispobj *)BINDING_STACK_START,
1356 (lispobj *)current_binding_stack_pointer - (lispobj *)BINDING_STACK_START,
1359 pscav( (lispobj *)BINDING_STACK_START,
1360 (lispobj *)SymbolValue(BINDING_STACK_POINTER) -
1361 (lispobj *)BINDING_STACK_START,
1365 /* The original CMU CL code had scavenge-read-only-space code
1366 * controlled by the Lisp-level variable
1367 * *SCAVENGE-READ-ONLY-SPACE*. It was disabled by default, and it
1368 * wasn't documented under what circumstances it was useful or
1369 * safe to turn it on, so it's been turned off in SBCL. If you
1370 * want/need this functionality, and can test and document it,
1371 * please submit a patch. */
1373 if (SymbolValue(SCAVENGE_READ_ONLY_SPACE) != type_UnboundMarker
1374 && SymbolValue(SCAVENGE_READ_ONLY_SPACE) != NIL) {
1375 unsigned read_only_space_size =
1376 (lispobj *)SymbolValue(READ_ONLY_SPACE_FREE_POINTER) -
1377 (lispobj *)READ_ONLY_SPACE_START;
1379 "scavenging read only space: %d bytes\n",
1380 read_only_space_size * sizeof(lispobj));
1381 pscav( (lispobj *)READ_ONLY_SPACE_START, read_only_space_size, 0);
1389 clean = (lispobj *)STATIC_SPACE_START;
1391 while (clean != static_free)
1392 clean = pscav(clean, static_free - clean, 0);
1393 laters = later_blocks;
1394 count = later_count;
1395 later_blocks = NULL;
1397 while (laters != NULL) {
1398 for (i = 0; i < count; i++) {
1399 if (laters->u[i].count == 0) {
1401 } else if (laters->u[i].count <= LATERMAXCOUNT) {
1402 pscav(laters->u[i+1].ptr, laters->u[i].count, 1);
1405 pscav(laters->u[i].ptr, 1, 1);
1408 next = laters->next;
1411 count = LATERBLOCKSIZE;
1413 } while (clean != static_free || later_blocks != NULL);
1420 os_zero((os_vm_address_t) current_dynamic_space,
1421 (os_vm_size_t) DYNAMIC_SPACE_SIZE);
1423 /* Zero the stack. Note that the stack is also zeroed by SUB-GC
1424 * calling SCRUB-CONTROL-STACK - this zeros the stack on the x86. */
1426 os_zero((os_vm_address_t) current_control_stack_pointer,
1427 (os_vm_size_t) (CONTROL_STACK_SIZE -
1428 ((current_control_stack_pointer -
1429 (lispobj *)CONTROL_STACK_START) *
1433 /* It helps to update the heap free pointers so that free_heap can
1434 * verify after it's done. */
1435 SetSymbolValue(READ_ONLY_SPACE_FREE_POINTER, (lispobj)read_only_free);
1436 SetSymbolValue(STATIC_SPACE_FREE_POINTER, (lispobj)static_free);
1438 #if !defined(__i386__)
1439 dynamic_space_free_pointer = current_dynamic_space;
1444 #error unsupported case /* in CMU CL, was "ibmrt using GC" */