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>
26 #include "interrupt.h"
36 /* again, what's so special about the x86 that this is differently
37 * visible there than on other platforms? -dan 20010125
39 static lispobj *dynamic_space_free_pointer;
43 lose("GC invariant lost, file \"%s\", line %d", __FILE__, __LINE__)
46 #define gc_assert(ex) do { \
47 if (!(ex)) gc_abort(); \
54 /* These hold the original end of the read_only and static spaces so
55 * we can tell what are forwarding pointers. */
57 static lispobj *read_only_end, *static_end;
59 static lispobj *read_only_free, *static_free;
61 static lispobj *pscav(lispobj *addr, int nwords, boolean constant);
63 #define LATERBLOCKSIZE 1020
64 #define LATERMAXCOUNT 10
73 } *later_blocks = NULL;
74 static int later_count = 0;
76 #define CEILING(x,y) (((x) + ((y) - 1)) & (~((y) - 1)))
77 #define NWORDS(x,y) (CEILING((x),(y)) / (y))
79 /* FIXME: (1) Shouldn't this be defined in sbcl.h? */
81 #define FUN_RAW_ADDR_OFFSET 0
83 #define FUN_RAW_ADDR_OFFSET (6*sizeof(lispobj) - FUN_POINTER_LOWTAG)
87 forwarding_pointer_p(lispobj obj)
93 return ((static_end <= ptr && ptr <= static_free) ||
94 (read_only_end <= ptr && ptr <= read_only_free));
98 dynamic_pointer_p(lispobj ptr)
101 return (ptr >= (lispobj)current_dynamic_space
103 ptr < (lispobj)dynamic_space_free_pointer);
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 (lowtag_of((lispobj)pointer)) {
150 case FUN_POINTER_LOWTAG:
151 /* Start_addr should be the enclosing code object, or a closure
153 switch (widetag_of(*start_addr)) {
154 case CODE_HEADER_WIDETAG:
155 /* This case is probably caught above. */
157 case CLOSURE_HEADER_WIDETAG:
158 case FUNCALLABLE_INSTANCE_HEADER_WIDETAG:
159 if ((int)pointer != ((int)start_addr+FUN_POINTER_LOWTAG)) {
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 LIST_POINTER_LOWTAG:
176 if ((int)pointer != ((int)start_addr+LIST_POINTER_LOWTAG)) {
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 || (widetag_of(start_addr[0]) == BASE_CHAR_WIDETAG)
186 || (widetag_of(start_addr[0]) == UNBOUND_MARKER_WIDETAG))
187 && (is_lisp_pointer(start_addr[1])
188 || ((start_addr[1] & 3) == 0) /* fixnum */
189 || (widetag_of(start_addr[1]) == BASE_CHAR_WIDETAG)
190 || (widetag_of(start_addr[1]) == UNBOUND_MARKER_WIDETAG))) {
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 INSTANCE_POINTER_LOWTAG:
200 if ((int)pointer != ((int)start_addr+INSTANCE_POINTER_LOWTAG)) {
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 (widetag_of(start_addr[0]) != INSTANCE_HEADER_WIDETAG) {
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 OTHER_POINTER_LOWTAG:
216 if ((int)pointer != ((int)start_addr+OTHER_POINTER_LOWTAG)) {
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. XXX 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 (widetag_of(start_addr[0])) {
232 case UNBOUND_MARKER_WIDETAG:
233 case BASE_CHAR_WIDETAG:
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 CLOSURE_HEADER_WIDETAG:
242 case FUNCALLABLE_INSTANCE_HEADER_WIDETAG:
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 INSTANCE_HEADER_WIDETAG:
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 SIMPLE_VECTOR_WIDETAG:
259 case COMPLEX_WIDETAG:
260 #ifdef COMPLEX_SINGLE_FLOAT_WIDETAG
261 case COMPLEX_SINGLE_FLOAT_WIDETAG:
263 #ifdef COMPLEX_DOUBLE_FLOAT_WIDETAG
264 case COMPLEX_DOUBLE_FLOAT_WIDETAG:
266 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
267 case COMPLEX_LONG_FLOAT_WIDETAG:
269 case SIMPLE_ARRAY_WIDETAG:
270 case COMPLEX_STRING_WIDETAG:
271 case COMPLEX_BIT_VECTOR_WIDETAG:
272 case COMPLEX_VECTOR_WIDETAG:
273 case COMPLEX_ARRAY_WIDETAG:
274 case VALUE_CELL_HEADER_WIDETAG:
275 case SYMBOL_HEADER_WIDETAG:
277 case CODE_HEADER_WIDETAG:
279 case SINGLE_FLOAT_WIDETAG:
280 case DOUBLE_FLOAT_WIDETAG:
281 #ifdef LONG_FLOAT_WIDETAG
282 case LONG_FLOAT_WIDETAG:
284 case SIMPLE_STRING_WIDETAG:
285 case SIMPLE_BIT_VECTOR_WIDETAG:
286 case SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG:
287 case SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG:
288 case SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG:
289 case SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG:
290 case SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG:
291 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
292 case SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG:
294 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
295 case SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG:
297 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG
298 case SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG:
300 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
301 case SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG:
303 case SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG:
304 case SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG:
305 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
306 case SIMPLE_ARRAY_LONG_FLOAT_WIDETAG:
308 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
309 case SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG:
311 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
312 case SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG:
314 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
315 case SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG:
318 case WEAK_POINTER_WIDETAG:
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 (widetag_of(*start_addr) == CODE_HEADER_WIDETAG) {
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 + OTHER_POINTER_LOWTAG);
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 | lowtag_of(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 | lowtag_of(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 | lowtag_of(thing);
545 /* Scavenge the function. */
546 fdefn = (struct fdefn *)new;
548 pscav(&fdefn->fun, 1, 0);
549 if ((char *)oldfn + FUN_RAW_ADDR_OFFSET == fdefn->raw_addr)
550 fdefn->raw_addr = (char *)fdefn->fun + FUN_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 | lowtag_of(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 | lowtag_of(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==UNBOUND_MARKER_WIDETAG) ||
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. */
652 (struct vector *)native_pointer(*(lispobj *)fixups_vector);
655 if (widetag_of(fixups_vector->header) ==
656 SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG) {
657 /* We got the fixups for the code block. Now work through the
658 * vector, and apply a fixup at each address. */
659 int length = fixnum_value(fixups_vector->length);
661 for (i=0; i<length; i++) {
662 unsigned offset = fixups_vector->data[i];
663 /* Now check the current value of offset. */
665 *(unsigned *)((unsigned)code_start_addr + offset);
667 /* If it's within the old_code object then it must be an
668 * absolute fixup (relative ones are not saved) */
669 if ((old_value>=(unsigned)old_code)
670 && (old_value<((unsigned)old_code + nwords*4)))
671 /* So add the dispacement. */
672 *(unsigned *)((unsigned)code_start_addr + offset) = old_value
675 /* It is outside the old code object so it must be a relative
676 * fixup (absolute fixups are not saved). So subtract the
678 *(unsigned *)((unsigned)code_start_addr + offset) = old_value
683 /* No longer need the fixups. */
684 new_code->constants[0] = 0;
687 /* Check for possible errors. */
688 sniff_code_object(new_code,displacement);
694 ptrans_code(lispobj thing)
696 struct code *code, *new;
698 lispobj func, result;
700 code = (struct code *)native_pointer(thing);
701 nwords = HeaderValue(code->header) + fixnum_value(code->code_size);
703 new = (struct code *)read_only_free;
704 read_only_free += CEILING(nwords, 2);
706 bcopy(code, new, nwords * sizeof(lispobj));
709 apply_code_fixups_during_purify(code,new);
712 result = (lispobj)new | OTHER_POINTER_LOWTAG;
714 /* Stick in a forwarding pointer for the code object. */
715 *(lispobj *)code = result;
717 /* Put in forwarding pointers for all the functions. */
718 for (func = code->entry_points;
720 func = ((struct simple_fun *)native_pointer(func))->next) {
722 gc_assert(lowtag_of(func) == FUN_POINTER_LOWTAG);
724 *(lispobj *)native_pointer(func) = result + (func - thing);
727 /* Arrange to scavenge the debug info later. */
728 pscav_later(&new->debug_info, 1);
730 if (new->trace_table_offset & 0x3)
732 pscav(&new->trace_table_offset, 1, 0);
734 new->trace_table_offset = NIL; /* limit lifetime */
737 /* Scavenge the constants. */
738 pscav(new->constants, HeaderValue(new->header)-5, 1);
740 /* Scavenge all the functions. */
741 pscav(&new->entry_points, 1, 1);
742 for (func = new->entry_points;
744 func = ((struct simple_fun *)native_pointer(func))->next) {
745 gc_assert(lowtag_of(func) == FUN_POINTER_LOWTAG);
746 gc_assert(!dynamic_pointer_p(func));
749 /* Temporarly convert the self pointer to a real function pointer. */
750 ((struct simple_fun *)native_pointer(func))->self
751 -= FUN_RAW_ADDR_OFFSET;
753 pscav(&((struct simple_fun *)native_pointer(func))->self, 2, 1);
755 ((struct simple_fun *)native_pointer(func))->self
756 += FUN_RAW_ADDR_OFFSET;
758 pscav_later(&((struct simple_fun *)native_pointer(func))->name, 3);
765 ptrans_func(lispobj thing, lispobj header)
768 lispobj code, *new, *old, result;
769 struct simple_fun *function;
771 /* Thing can either be a function header, a closure function
772 * header, a closure, or a funcallable-instance. If it's a closure
773 * or a funcallable-instance, we do the same as ptrans_boxed.
774 * Otherwise we have to do something strange, 'cause it is buried
775 * inside a code object. */
777 if (widetag_of(header) == SIMPLE_FUN_HEADER_WIDETAG ||
778 widetag_of(header) == CLOSURE_FUN_HEADER_WIDETAG) {
780 /* We can only end up here if the code object has not been
781 * scavenged, because if it had been scavenged, forwarding pointers
782 * would have been left behind for all the entry points. */
784 function = (struct simple_fun *)native_pointer(thing);
786 (native_pointer(thing) -
787 (HeaderValue(function->header)*sizeof(lispobj))) |
788 OTHER_POINTER_LOWTAG;
790 /* This will cause the function's header to be replaced with a
791 * forwarding pointer. */
794 /* So we can just return that. */
795 return function->header;
798 /* It's some kind of closure-like thing. */
799 nwords = 1 + HeaderValue(header);
800 old = (lispobj *)native_pointer(thing);
802 /* Allocate the new one. */
803 if (widetag_of(header) == FUNCALLABLE_INSTANCE_HEADER_WIDETAG) {
804 /* FINs *must* not go in read_only space. */
806 static_free += CEILING(nwords, 2);
809 /* Closures can always go in read-only space, 'cause they
812 new = read_only_free;
813 read_only_free += CEILING(nwords, 2);
816 bcopy(old, new, nwords * sizeof(lispobj));
818 /* Deposit forwarding pointer. */
819 result = (lispobj)new | lowtag_of(thing);
823 pscav(new, nwords, 0);
830 ptrans_returnpc(lispobj thing, lispobj header)
834 /* Find the corresponding code object. */
835 code = thing - HeaderValue(header)*sizeof(lispobj);
837 /* Make sure it's been transported. */
838 new = *(lispobj *)native_pointer(code);
839 if (!forwarding_pointer_p(new))
840 new = ptrans_code(code);
842 /* Maintain the offset: */
843 return new + (thing - code);
846 #define WORDS_PER_CONS CEILING(sizeof(struct cons) / sizeof(lispobj), 2)
849 ptrans_list(lispobj thing, boolean constant)
851 struct cons *old, *new, *orig;
855 orig = (struct cons *)read_only_free;
857 orig = (struct cons *)static_free;
861 /* Allocate a new cons cell. */
862 old = (struct cons *)native_pointer(thing);
864 new = (struct cons *)read_only_free;
865 read_only_free += WORDS_PER_CONS;
868 new = (struct cons *)static_free;
869 static_free += WORDS_PER_CONS;
872 /* Copy the cons cell and keep a pointer to the cdr. */
874 thing = new->cdr = old->cdr;
876 /* Set up the forwarding pointer. */
877 *(lispobj *)old = ((lispobj)new) | LIST_POINTER_LOWTAG;
879 /* And count this cell. */
881 } while (lowtag_of(thing) == LIST_POINTER_LOWTAG &&
882 dynamic_pointer_p(thing) &&
883 !(forwarding_pointer_p(*(lispobj *)native_pointer(thing))));
885 /* Scavenge the list we just copied. */
886 pscav((lispobj *)orig, length * WORDS_PER_CONS, constant);
888 return ((lispobj)orig) | LIST_POINTER_LOWTAG;
892 ptrans_otherptr(lispobj thing, lispobj header, boolean constant)
894 switch (widetag_of(header)) {
896 case SINGLE_FLOAT_WIDETAG:
897 case DOUBLE_FLOAT_WIDETAG:
898 #ifdef LONG_FLOAT_WIDETAG
899 case LONG_FLOAT_WIDETAG:
901 #ifdef COMPLEX_SINGLE_FLOAT_WIDETAG
902 case COMPLEX_SINGLE_FLOAT_WIDETAG:
904 #ifdef COMPLEX_DOUBLE_FLOAT_WIDETAG
905 case COMPLEX_DOUBLE_FLOAT_WIDETAG:
907 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
908 case COMPLEX_LONG_FLOAT_WIDETAG:
911 return ptrans_unboxed(thing, header);
914 case COMPLEX_WIDETAG:
915 case SIMPLE_ARRAY_WIDETAG:
916 case COMPLEX_STRING_WIDETAG:
917 case COMPLEX_VECTOR_WIDETAG:
918 case COMPLEX_ARRAY_WIDETAG:
919 return ptrans_boxed(thing, header, constant);
921 case VALUE_CELL_HEADER_WIDETAG:
922 case WEAK_POINTER_WIDETAG:
923 return ptrans_boxed(thing, header, 0);
925 case SYMBOL_HEADER_WIDETAG:
926 return ptrans_boxed(thing, header, 0);
928 case SIMPLE_STRING_WIDETAG:
929 return ptrans_vector(thing, 8, 1, 0, constant);
931 case SIMPLE_BIT_VECTOR_WIDETAG:
932 return ptrans_vector(thing, 1, 0, 0, constant);
934 case SIMPLE_VECTOR_WIDETAG:
935 return ptrans_vector(thing, 32, 0, 1, constant);
937 case SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG:
938 return ptrans_vector(thing, 2, 0, 0, constant);
940 case SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG:
941 return ptrans_vector(thing, 4, 0, 0, constant);
943 case SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG:
944 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
945 case SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG:
947 return ptrans_vector(thing, 8, 0, 0, constant);
949 case SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG:
950 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
951 case SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG:
953 return ptrans_vector(thing, 16, 0, 0, constant);
955 case SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG:
956 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG
957 case SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG:
959 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
960 case SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG:
962 return ptrans_vector(thing, 32, 0, 0, constant);
964 case SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG:
965 return ptrans_vector(thing, 32, 0, 0, constant);
967 case SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG:
968 return ptrans_vector(thing, 64, 0, 0, constant);
970 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
971 case SIMPLE_ARRAY_LONG_FLOAT_WIDETAG:
973 return ptrans_vector(thing, 96, 0, 0, constant);
976 return ptrans_vector(thing, 128, 0, 0, constant);
980 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
981 case SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG:
982 return ptrans_vector(thing, 64, 0, 0, constant);
985 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
986 case SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG:
987 return ptrans_vector(thing, 128, 0, 0, constant);
990 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
991 case SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG:
993 return ptrans_vector(thing, 192, 0, 0, constant);
996 return ptrans_vector(thing, 256, 0, 0, constant);
1000 case CODE_HEADER_WIDETAG:
1001 return ptrans_code(thing);
1003 case RETURN_PC_HEADER_WIDETAG:
1004 return ptrans_returnpc(thing, header);
1007 return ptrans_fdefn(thing, header);
1010 /* Should only come across other pointers to the above stuff. */
1017 pscav_fdefn(struct fdefn *fdefn)
1021 fix_func = ((char *)(fdefn->fun+FUN_RAW_ADDR_OFFSET) == fdefn->raw_addr);
1022 pscav(&fdefn->name, 1, 1);
1023 pscav(&fdefn->fun, 1, 0);
1025 fdefn->raw_addr = (char *)(fdefn->fun + FUN_RAW_ADDR_OFFSET);
1026 return sizeof(struct fdefn) / sizeof(lispobj);
1030 /* now putting code objects in static space */
1032 pscav_code(struct code*code)
1036 nwords = HeaderValue(code->header) + fixnum_value(code->code_size);
1038 /* Arrange to scavenge the debug info later. */
1039 pscav_later(&code->debug_info, 1);
1041 /* Scavenge the constants. */
1042 pscav(code->constants, HeaderValue(code->header)-5, 1);
1044 /* Scavenge all the functions. */
1045 pscav(&code->entry_points, 1, 1);
1046 for (func = code->entry_points;
1048 func = ((struct simple_fun *)native_pointer(func))->next) {
1049 gc_assert(lowtag_of(func) == FUN_POINTER_LOWTAG);
1050 gc_assert(!dynamic_pointer_p(func));
1053 /* Temporarly convert the self pointer to a real function
1055 ((struct simple_fun *)native_pointer(func))->self
1056 -= FUN_RAW_ADDR_OFFSET;
1058 pscav(&((struct simple_fun *)native_pointer(func))->self, 2, 1);
1060 ((struct simple_fun *)native_pointer(func))->self
1061 += FUN_RAW_ADDR_OFFSET;
1063 pscav_later(&((struct simple_fun *)native_pointer(func))->name, 3);
1066 return CEILING(nwords,2);
1071 pscav(lispobj *addr, int nwords, boolean constant)
1073 lispobj thing, *thingp, header;
1074 int count = 0; /* (0 = dummy init value to stop GCC warning) */
1075 struct vector *vector;
1077 while (nwords > 0) {
1079 if (is_lisp_pointer(thing)) {
1080 /* It's a pointer. Is it something we might have to move? */
1081 if (dynamic_pointer_p(thing)) {
1082 /* Maybe. Have we already moved it? */
1083 thingp = (lispobj *)native_pointer(thing);
1085 if (is_lisp_pointer(header) && forwarding_pointer_p(header))
1086 /* Yep, so just copy the forwarding pointer. */
1089 /* Nope, copy the object. */
1090 switch (lowtag_of(thing)) {
1091 case FUN_POINTER_LOWTAG:
1092 thing = ptrans_func(thing, header);
1095 case LIST_POINTER_LOWTAG:
1096 thing = ptrans_list(thing, constant);
1099 case INSTANCE_POINTER_LOWTAG:
1100 thing = ptrans_instance(thing, header, constant);
1103 case OTHER_POINTER_LOWTAG:
1104 thing = ptrans_otherptr(thing, header, constant);
1108 /* It was a pointer, but not one of them? */
1116 else if (thing & 3) {
1117 /* It's an other immediate. Maybe the header for an unboxed */
1119 switch (widetag_of(thing)) {
1120 case BIGNUM_WIDETAG:
1121 case SINGLE_FLOAT_WIDETAG:
1122 case DOUBLE_FLOAT_WIDETAG:
1123 #ifdef LONG_FLOAT_WIDETAG
1124 case LONG_FLOAT_WIDETAG:
1127 /* It's an unboxed simple object. */
1128 count = HeaderValue(thing)+1;
1131 case SIMPLE_VECTOR_WIDETAG:
1132 if (HeaderValue(thing) == subtype_VectorValidHashing) {
1133 *addr = (subtype_VectorMustRehash << N_WIDETAG_BITS) |
1134 SIMPLE_VECTOR_WIDETAG;
1139 case SIMPLE_STRING_WIDETAG:
1140 vector = (struct vector *)addr;
1141 count = CEILING(NWORDS(fixnum_value(vector->length)+1,4)+2,2);
1144 case SIMPLE_BIT_VECTOR_WIDETAG:
1145 vector = (struct vector *)addr;
1146 count = CEILING(NWORDS(fixnum_value(vector->length),32)+2,2);
1149 case SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG:
1150 vector = (struct vector *)addr;
1151 count = CEILING(NWORDS(fixnum_value(vector->length),16)+2,2);
1154 case SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG:
1155 vector = (struct vector *)addr;
1156 count = CEILING(NWORDS(fixnum_value(vector->length),8)+2,2);
1159 case SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG:
1160 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
1161 case SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG:
1163 vector = (struct vector *)addr;
1164 count = CEILING(NWORDS(fixnum_value(vector->length),4)+2,2);
1167 case SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG:
1168 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
1169 case SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG:
1171 vector = (struct vector *)addr;
1172 count = CEILING(NWORDS(fixnum_value(vector->length),2)+2,2);
1175 case SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG:
1176 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG
1177 case SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG:
1179 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
1180 case SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG:
1182 vector = (struct vector *)addr;
1183 count = CEILING(fixnum_value(vector->length)+2,2);
1186 case SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG:
1187 vector = (struct vector *)addr;
1188 count = CEILING(fixnum_value(vector->length)+2,2);
1191 case SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG:
1192 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
1193 case SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG:
1195 vector = (struct vector *)addr;
1196 count = fixnum_value(vector->length)*2+2;
1199 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
1200 case SIMPLE_ARRAY_LONG_FLOAT_WIDETAG:
1201 vector = (struct vector *)addr;
1203 count = fixnum_value(vector->length)*3+2;
1206 count = fixnum_value(vector->length)*4+2;
1211 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
1212 case SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG:
1213 vector = (struct vector *)addr;
1214 count = fixnum_value(vector->length)*4+2;
1218 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
1219 case SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG:
1220 vector = (struct vector *)addr;
1222 count = fixnum_value(vector->length)*6+2;
1225 count = fixnum_value(vector->length)*8+2;
1230 case CODE_HEADER_WIDETAG:
1232 gc_abort(); /* no code headers in static space */
1234 count = pscav_code((struct code*)addr);
1238 case SIMPLE_FUN_HEADER_WIDETAG:
1239 case CLOSURE_FUN_HEADER_WIDETAG:
1240 case RETURN_PC_HEADER_WIDETAG:
1241 /* We should never hit any of these, 'cause they occur
1242 * buried in the middle of code objects. */
1247 case CLOSURE_HEADER_WIDETAG:
1248 case FUNCALLABLE_INSTANCE_HEADER_WIDETAG:
1249 /* The function self pointer needs special care on the
1250 * x86 because it is the real entry point. */
1252 lispobj fun = ((struct closure *)addr)->fun
1253 - FUN_RAW_ADDR_OFFSET;
1254 pscav(&fun, 1, constant);
1255 ((struct closure *)addr)->fun = fun + FUN_RAW_ADDR_OFFSET;
1261 case WEAK_POINTER_WIDETAG:
1262 /* Weak pointers get preserved during purify, 'cause I
1263 * don't feel like figuring out how to break them. */
1264 pscav(addr+1, 2, constant);
1269 /* We have to handle fdefn objects specially, so we
1270 * can fix up the raw function address. */
1271 count = pscav_fdefn((struct fdefn *)addr);
1280 /* It's a fixnum. */
1292 purify(lispobj static_roots, lispobj read_only_roots)
1296 struct later *laters, *next;
1299 printf("[doing purification:");
1303 if (fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX)) != 0) {
1304 /* FIXME: 1. What does this mean? 2. It shouldn't be reporting
1305 * its error simply by a. printing a string b. to stdout instead
1307 printf(" Ack! Can't purify interrupt contexts. ");
1312 #if defined(__i386__)
1313 dynamic_space_free_pointer =
1314 (lispobj*)SymbolValue(ALLOCATION_POINTER);
1317 read_only_end = read_only_free =
1318 (lispobj *)SymbolValue(READ_ONLY_SPACE_FREE_POINTER);
1319 static_end = static_free =
1320 (lispobj *)SymbolValue(STATIC_SPACE_FREE_POINTER);
1328 gc_assert((lispobj *)CONTROL_STACK_END > ((&read_only_roots)+1));
1329 setup_i386_stack_scav(((&static_roots)-2), (lispobj *)CONTROL_STACK_END);
1332 pscav(&static_roots, 1, 0);
1333 pscav(&read_only_roots, 1, 1);
1336 printf(" handlers");
1339 pscav((lispobj *) interrupt_handlers,
1340 sizeof(interrupt_handlers) / sizeof(lispobj),
1348 pscav((lispobj *)CONTROL_STACK_START,
1349 current_control_stack_pointer - (lispobj *)CONTROL_STACK_START,
1358 printf(" bindings");
1361 #if !defined(__i386__)
1362 pscav( (lispobj *)BINDING_STACK_START,
1363 (lispobj *)current_binding_stack_pointer - (lispobj *)BINDING_STACK_START,
1366 pscav( (lispobj *)BINDING_STACK_START,
1367 (lispobj *)SymbolValue(BINDING_STACK_POINTER) -
1368 (lispobj *)BINDING_STACK_START,
1372 /* The original CMU CL code had scavenge-read-only-space code
1373 * controlled by the Lisp-level variable
1374 * *SCAVENGE-READ-ONLY-SPACE*. It was disabled by default, and it
1375 * wasn't documented under what circumstances it was useful or
1376 * safe to turn it on, so it's been turned off in SBCL. If you
1377 * want/need this functionality, and can test and document it,
1378 * please submit a patch. */
1380 if (SymbolValue(SCAVENGE_READ_ONLY_SPACE) != UNBOUND_MARKER_WIDETAG
1381 && SymbolValue(SCAVENGE_READ_ONLY_SPACE) != NIL) {
1382 unsigned read_only_space_size =
1383 (lispobj *)SymbolValue(READ_ONLY_SPACE_FREE_POINTER) -
1384 (lispobj *)READ_ONLY_SPACE_START;
1386 "scavenging read only space: %d bytes\n",
1387 read_only_space_size * sizeof(lispobj));
1388 pscav( (lispobj *)READ_ONLY_SPACE_START, read_only_space_size, 0);
1396 clean = (lispobj *)STATIC_SPACE_START;
1398 while (clean != static_free)
1399 clean = pscav(clean, static_free - clean, 0);
1400 laters = later_blocks;
1401 count = later_count;
1402 later_blocks = NULL;
1404 while (laters != NULL) {
1405 for (i = 0; i < count; i++) {
1406 if (laters->u[i].count == 0) {
1408 } else if (laters->u[i].count <= LATERMAXCOUNT) {
1409 pscav(laters->u[i+1].ptr, laters->u[i].count, 1);
1412 pscav(laters->u[i].ptr, 1, 1);
1415 next = laters->next;
1418 count = LATERBLOCKSIZE;
1420 } while (clean != static_free || later_blocks != NULL);
1427 os_zero((os_vm_address_t) current_dynamic_space,
1428 (os_vm_size_t) DYNAMIC_SPACE_SIZE);
1430 /* Zero the stack. Note that the stack is also zeroed by SUB-GC
1431 * calling SCRUB-CONTROL-STACK - this zeros the stack on the x86. */
1433 os_zero((os_vm_address_t) current_control_stack_pointer,
1434 (os_vm_size_t) (CONTROL_STACK_SIZE -
1435 ((current_control_stack_pointer -
1436 (lispobj *)CONTROL_STACK_START) *
1440 /* It helps to update the heap free pointers so that free_heap can
1441 * verify after it's done. */
1442 SetSymbolValue(READ_ONLY_SPACE_FREE_POINTER, (lispobj)read_only_free);
1443 SetSymbolValue(STATIC_SPACE_FREE_POINTER, (lispobj)static_free);
1445 #if !defined(__i386__)
1446 dynamic_space_free_pointer = current_dynamic_space;
1451 #error unsupported case /* in CMU CL, was "ibmrt using GC" */