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 return (ptr
>= (lispobj
)current_dynamic_space
102 ptr
< (lispobj
)dynamic_space_free_pointer
);
104 /* Be more conservative, and remember, this is a maybe. */
105 return (ptr
>= (lispobj
)DYNAMIC_SPACE_START
107 ptr
< (lispobj
)dynamic_space_free_pointer
);
116 * enhanced x86/GENCGC stack scavenging by Douglas Crosher
118 * Scavenging the stack on the i386 is problematic due to conservative
119 * roots and raw return addresses. Here it is handled in two passes:
120 * the first pass runs before any objects are moved and tries to
121 * identify valid pointers and return address on the stack, the second
122 * pass scavenges these.
125 static unsigned pointer_filter_verbose
= 0;
127 /* FIXME: This is substantially the same code as in gencgc.c. (There
128 * are some differences, at least (1) the gencgc.c code needs to worry
129 * about return addresses on the stack pinning code objects, (2) the
130 * gencgc.c code needs to worry about the GC maybe happening in an
131 * interrupt service routine when the main thread of control was
132 * interrupted just as it had allocated memory and before it
133 * initialized it, while PURIFY needn't worry about that, and (3) the
134 * gencgc.c code has mutated more under maintenance since the fork
135 * from CMU CL than the code here has.) The two versions should be
136 * made to explicitly share common code, instead of just two different
137 * cut-and-pasted versions. */
139 valid_dynamic_space_pointer(lispobj
*pointer
, lispobj
*start_addr
)
141 /* If it's not a return address then it needs to be a valid Lisp
143 if (!is_lisp_pointer((lispobj
)pointer
))
146 /* Check that the object pointed to is consistent with the pointer
148 switch (lowtag_of((lispobj
)pointer
)) {
149 case FUN_POINTER_LOWTAG
:
150 /* Start_addr should be the enclosing code object, or a closure
152 switch (widetag_of(*start_addr
)) {
153 case CODE_HEADER_WIDETAG
:
154 /* This case is probably caught above. */
156 case CLOSURE_HEADER_WIDETAG
:
157 case FUNCALLABLE_INSTANCE_HEADER_WIDETAG
:
158 if ((int)pointer
!= ((int)start_addr
+FUN_POINTER_LOWTAG
)) {
159 if (pointer_filter_verbose
) {
160 fprintf(stderr
,"*Wf2: %x %x %x\n", (unsigned int) pointer
,
161 (unsigned int) start_addr
, *start_addr
);
167 if (pointer_filter_verbose
) {
168 fprintf(stderr
,"*Wf3: %x %x %x\n", (unsigned int) pointer
,
169 (unsigned int) start_addr
, *start_addr
);
174 case LIST_POINTER_LOWTAG
:
175 if ((int)pointer
!= ((int)start_addr
+LIST_POINTER_LOWTAG
)) {
176 if (pointer_filter_verbose
)
177 fprintf(stderr
,"*Wl1: %x %x %x\n", (unsigned int) pointer
,
178 (unsigned int) start_addr
, *start_addr
);
181 /* Is it plausible cons? */
182 if ((is_lisp_pointer(start_addr
[0])
183 || ((start_addr
[0] & 3) == 0) /* fixnum */
184 || (widetag_of(start_addr
[0]) == BASE_CHAR_WIDETAG
)
185 || (widetag_of(start_addr
[0]) == UNBOUND_MARKER_WIDETAG
))
186 && (is_lisp_pointer(start_addr
[1])
187 || ((start_addr
[1] & 3) == 0) /* fixnum */
188 || (widetag_of(start_addr
[1]) == BASE_CHAR_WIDETAG
)
189 || (widetag_of(start_addr
[1]) == UNBOUND_MARKER_WIDETAG
))) {
192 if (pointer_filter_verbose
) {
193 fprintf(stderr
,"*Wl2: %x %x %x\n", (unsigned int) pointer
,
194 (unsigned int) start_addr
, *start_addr
);
198 case INSTANCE_POINTER_LOWTAG
:
199 if ((int)pointer
!= ((int)start_addr
+INSTANCE_POINTER_LOWTAG
)) {
200 if (pointer_filter_verbose
) {
201 fprintf(stderr
,"*Wi1: %x %x %x\n", (unsigned int) pointer
,
202 (unsigned int) start_addr
, *start_addr
);
206 if (widetag_of(start_addr
[0]) != INSTANCE_HEADER_WIDETAG
) {
207 if (pointer_filter_verbose
) {
208 fprintf(stderr
,"*Wi2: %x %x %x\n", (unsigned int) pointer
,
209 (unsigned int) start_addr
, *start_addr
);
214 case OTHER_POINTER_LOWTAG
:
215 if ((int)pointer
!= ((int)start_addr
+OTHER_POINTER_LOWTAG
)) {
216 if (pointer_filter_verbose
) {
217 fprintf(stderr
,"*Wo1: %x %x %x\n", (unsigned int) pointer
,
218 (unsigned int) start_addr
, *start_addr
);
222 /* Is it plausible? Not a cons. XXX should check the headers. */
223 if (is_lisp_pointer(start_addr
[0]) || ((start_addr
[0] & 3) == 0)) {
224 if (pointer_filter_verbose
) {
225 fprintf(stderr
,"*Wo2: %x %x %x\n", (unsigned int) pointer
,
226 (unsigned int) start_addr
, *start_addr
);
230 switch (widetag_of(start_addr
[0])) {
231 case UNBOUND_MARKER_WIDETAG
:
232 case BASE_CHAR_WIDETAG
:
233 if (pointer_filter_verbose
) {
234 fprintf(stderr
,"*Wo3: %x %x %x\n", (unsigned int) pointer
,
235 (unsigned int) start_addr
, *start_addr
);
239 /* only pointed to by function pointers? */
240 case CLOSURE_HEADER_WIDETAG
:
241 case FUNCALLABLE_INSTANCE_HEADER_WIDETAG
:
242 if (pointer_filter_verbose
) {
243 fprintf(stderr
,"*Wo4: %x %x %x\n", (unsigned int) pointer
,
244 (unsigned int) start_addr
, *start_addr
);
248 case INSTANCE_HEADER_WIDETAG
:
249 if (pointer_filter_verbose
) {
250 fprintf(stderr
,"*Wo5: %x %x %x\n", (unsigned int) pointer
,
251 (unsigned int) start_addr
, *start_addr
);
255 /* the valid other immediate pointer objects */
256 case SIMPLE_VECTOR_WIDETAG
:
258 case COMPLEX_WIDETAG
:
259 #ifdef COMPLEX_SINGLE_FLOAT_WIDETAG
260 case COMPLEX_SINGLE_FLOAT_WIDETAG
:
262 #ifdef COMPLEX_DOUBLE_FLOAT_WIDETAG
263 case COMPLEX_DOUBLE_FLOAT_WIDETAG
:
265 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
266 case COMPLEX_LONG_FLOAT_WIDETAG
:
268 case SIMPLE_ARRAY_WIDETAG
:
269 case COMPLEX_STRING_WIDETAG
:
270 case COMPLEX_BIT_VECTOR_WIDETAG
:
271 case COMPLEX_VECTOR_WIDETAG
:
272 case COMPLEX_ARRAY_WIDETAG
:
273 case VALUE_CELL_HEADER_WIDETAG
:
274 case SYMBOL_HEADER_WIDETAG
:
276 case CODE_HEADER_WIDETAG
:
278 case SINGLE_FLOAT_WIDETAG
:
279 case DOUBLE_FLOAT_WIDETAG
:
280 #ifdef LONG_FLOAT_WIDETAG
281 case LONG_FLOAT_WIDETAG
:
283 case SIMPLE_STRING_WIDETAG
:
284 case SIMPLE_BIT_VECTOR_WIDETAG
:
285 case SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG
:
286 case SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG
:
287 case SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG
:
288 case SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG
:
289 case SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG
:
290 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
291 case SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
:
293 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
294 case SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
:
296 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG
297 case SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG
:
299 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
300 case SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
:
302 case SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG
:
303 case SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG
:
304 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
305 case SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
:
307 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
308 case SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
:
310 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
311 case SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
:
313 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
314 case SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
:
317 case WEAK_POINTER_WIDETAG
:
321 if (pointer_filter_verbose
) {
322 fprintf(stderr
,"*Wo6: %x %x %x\n", (unsigned int) pointer
,
323 (unsigned int) start_addr
, *start_addr
);
329 if (pointer_filter_verbose
) {
330 fprintf(stderr
,"*W?: %x %x %x\n", (unsigned int) pointer
,
331 (unsigned int) start_addr
, *start_addr
);
340 #define MAX_STACK_POINTERS 256
341 lispobj
*valid_stack_locations
[MAX_STACK_POINTERS
];
342 unsigned int num_valid_stack_locations
;
344 #define MAX_STACK_RETURN_ADDRESSES 128
345 lispobj
*valid_stack_ra_locations
[MAX_STACK_RETURN_ADDRESSES
];
346 lispobj
*valid_stack_ra_code_objects
[MAX_STACK_RETURN_ADDRESSES
];
347 unsigned int num_valid_stack_ra_locations
;
349 /* Identify valid stack slots. */
351 setup_i386_stack_scav(lispobj
*lowaddr
, lispobj
*base
)
353 lispobj
*sp
= lowaddr
;
354 num_valid_stack_locations
= 0;
355 num_valid_stack_ra_locations
= 0;
356 for (sp
= lowaddr
; sp
< base
; sp
++) {
358 /* Find the object start address */
359 lispobj
*start_addr
= search_dynamic_space((void *)thing
);
361 /* We need to allow raw pointers into Code objects for
362 * return addresses. This will also pick up pointers to
363 * functions in code objects. */
364 if (widetag_of(*start_addr
) == CODE_HEADER_WIDETAG
) {
365 gc_assert(num_valid_stack_ra_locations
<
366 MAX_STACK_RETURN_ADDRESSES
);
367 valid_stack_ra_locations
[num_valid_stack_ra_locations
] = sp
;
368 valid_stack_ra_code_objects
[num_valid_stack_ra_locations
++] =
369 (lispobj
*)((int)start_addr
+ OTHER_POINTER_LOWTAG
);
371 if (valid_dynamic_space_pointer((void *)thing
, start_addr
)) {
372 gc_assert(num_valid_stack_locations
< MAX_STACK_POINTERS
);
373 valid_stack_locations
[num_valid_stack_locations
++] = sp
;
378 if (pointer_filter_verbose
) {
379 fprintf(stderr
, "number of valid stack pointers = %d\n",
380 num_valid_stack_locations
);
381 fprintf(stderr
, "number of stack return addresses = %d\n",
382 num_valid_stack_ra_locations
);
387 pscav_i386_stack(void)
391 for (i
= 0; i
< num_valid_stack_locations
; i
++)
392 pscav(valid_stack_locations
[i
], 1, 0);
394 for (i
= 0; i
< num_valid_stack_ra_locations
; i
++) {
395 lispobj code_obj
= (lispobj
)valid_stack_ra_code_objects
[i
];
396 pscav(&code_obj
, 1, 0);
397 if (pointer_filter_verbose
) {
398 fprintf(stderr
,"*C moved RA %x to %x; for code object %x to %x\n",
399 *valid_stack_ra_locations
[i
],
400 (int)(*valid_stack_ra_locations
[i
])
401 - ((int)valid_stack_ra_code_objects
[i
] - (int)code_obj
),
402 (unsigned int) valid_stack_ra_code_objects
[i
], code_obj
);
404 *valid_stack_ra_locations
[i
] =
405 ((int)(*valid_stack_ra_locations
[i
])
406 - ((int)valid_stack_ra_code_objects
[i
] - (int)code_obj
));
414 pscav_later(lispobj
*where
, int count
)
418 if (count
> LATERMAXCOUNT
) {
419 while (count
> LATERMAXCOUNT
) {
420 pscav_later(where
, LATERMAXCOUNT
);
421 count
-= LATERMAXCOUNT
;
422 where
+= LATERMAXCOUNT
;
426 if (later_blocks
== NULL
|| later_count
== LATERBLOCKSIZE
||
427 (later_count
== LATERBLOCKSIZE
-1 && count
> 1)) {
428 new = (struct later
*)malloc(sizeof(struct later
));
429 new->next
= later_blocks
;
430 if (later_blocks
&& later_count
< LATERBLOCKSIZE
)
431 later_blocks
->u
[later_count
].ptr
= NULL
;
437 later_blocks
->u
[later_count
++].count
= count
;
438 later_blocks
->u
[later_count
++].ptr
= where
;
443 ptrans_boxed(lispobj thing
, lispobj header
, boolean constant
)
446 lispobj result
, *new, *old
;
448 nwords
= 1 + HeaderValue(header
);
451 old
= (lispobj
*)native_pointer(thing
);
453 new = read_only_free
;
454 read_only_free
+= CEILING(nwords
, 2);
458 static_free
+= CEILING(nwords
, 2);
462 bcopy(old
, new, nwords
* sizeof(lispobj
));
464 /* Deposit forwarding pointer. */
465 result
= (lispobj
)new | lowtag_of(thing
);
469 pscav(new, nwords
, constant
);
474 /* We need to look at the layout to see whether it is a pure structure
475 * class, and only then can we transport as constant. If it is pure,
476 * we can ALWAYS transport as a constant. */
478 ptrans_instance(lispobj thing
, lispobj header
, boolean constant
)
480 lispobj layout
= ((struct instance
*)native_pointer(thing
))->slots
[0];
481 lispobj pure
= ((struct instance
*)native_pointer(layout
))->slots
[15];
485 return (ptrans_boxed(thing
, header
, 1));
487 return (ptrans_boxed(thing
, header
, 0));
490 /* Substructure: special case for the COMPACT-INFO-ENVs,
491 * where the instance may have a point to the dynamic
492 * space placed into it (e.g. the cache-name slot), but
493 * the lists and arrays at the time of a purify can be
494 * moved to the RO space. */
496 lispobj result
, *new, *old
;
498 nwords
= 1 + HeaderValue(header
);
501 old
= (lispobj
*)native_pointer(thing
);
503 static_free
+= CEILING(nwords
, 2);
506 bcopy(old
, new, nwords
* sizeof(lispobj
));
508 /* Deposit forwarding pointer. */
509 result
= (lispobj
)new | lowtag_of(thing
);
513 pscav(new, nwords
, 1);
519 return NIL
; /* dummy value: return something ... */
524 ptrans_fdefn(lispobj thing
, lispobj header
)
527 lispobj result
, *new, *old
, oldfn
;
530 nwords
= 1 + HeaderValue(header
);
533 old
= (lispobj
*)native_pointer(thing
);
535 static_free
+= CEILING(nwords
, 2);
538 bcopy(old
, new, nwords
* sizeof(lispobj
));
540 /* Deposit forwarding pointer. */
541 result
= (lispobj
)new | lowtag_of(thing
);
544 /* Scavenge the function. */
545 fdefn
= (struct fdefn
*)new;
547 pscav(&fdefn
->fun
, 1, 0);
548 if ((char *)oldfn
+ FUN_RAW_ADDR_OFFSET
== fdefn
->raw_addr
)
549 fdefn
->raw_addr
= (char *)fdefn
->fun
+ FUN_RAW_ADDR_OFFSET
;
555 ptrans_unboxed(lispobj thing
, lispobj header
)
558 lispobj result
, *new, *old
;
560 nwords
= 1 + HeaderValue(header
);
563 old
= (lispobj
*)native_pointer(thing
);
564 new = read_only_free
;
565 read_only_free
+= CEILING(nwords
, 2);
568 bcopy(old
, new, nwords
* sizeof(lispobj
));
570 /* Deposit forwarding pointer. */
571 result
= (lispobj
)new | lowtag_of(thing
);
578 ptrans_vector(lispobj thing
, int bits
, int extra
,
579 boolean boxed
, boolean constant
)
581 struct vector
*vector
;
583 lispobj result
, *new;
585 vector
= (struct vector
*)native_pointer(thing
);
586 nwords
= 2 + (CEILING((fixnum_value(vector
->length
)+extra
)*bits
,32)>>5);
588 if (boxed
&& !constant
) {
590 static_free
+= CEILING(nwords
, 2);
593 new = read_only_free
;
594 read_only_free
+= CEILING(nwords
, 2);
597 bcopy(vector
, new, nwords
* sizeof(lispobj
));
599 result
= (lispobj
)new | lowtag_of(thing
);
600 vector
->header
= result
;
603 pscav(new, nwords
, constant
);
610 apply_code_fixups_during_purify(struct code
*old_code
, struct code
*new_code
)
612 int nheader_words
, ncode_words
, nwords
;
613 void *constants_start_addr
, *constants_end_addr
;
614 void *code_start_addr
, *code_end_addr
;
615 lispobj fixups
= NIL
;
616 unsigned displacement
= (unsigned)new_code
- (unsigned)old_code
;
617 struct vector
*fixups_vector
;
619 ncode_words
= fixnum_value(new_code
->code_size
);
620 nheader_words
= HeaderValue(*(lispobj
*)new_code
);
621 nwords
= ncode_words
+ nheader_words
;
623 constants_start_addr
= (void *)new_code
+ 5*4;
624 constants_end_addr
= (void *)new_code
+ nheader_words
*4;
625 code_start_addr
= (void *)new_code
+ nheader_words
*4;
626 code_end_addr
= (void *)new_code
+ nwords
*4;
628 /* The first constant should be a pointer to the fixups for this
629 * code objects. Check. */
630 fixups
= new_code
->constants
[0];
632 /* It will be 0 or the unbound-marker if there are no fixups, and
633 * will be an other-pointer to a vector if it is valid. */
635 (fixups
==UNBOUND_MARKER_WIDETAG
) ||
636 !is_lisp_pointer(fixups
)) {
638 /* Check for a possible errors. */
639 sniff_code_object(new_code
,displacement
);
644 fixups_vector
= (struct vector
*)native_pointer(fixups
);
646 /* Could be pointing to a forwarding pointer. */
647 if (is_lisp_pointer(fixups
) && (dynamic_pointer_p(fixups
))
648 && forwarding_pointer_p(*(lispobj
*)fixups_vector
)) {
649 /* If so then follow it. */
651 (struct vector
*)native_pointer(*(lispobj
*)fixups_vector
);
654 if (widetag_of(fixups_vector
->header
) ==
655 SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG
) {
656 /* We got the fixups for the code block. Now work through the
657 * vector, 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. */
664 *(unsigned *)((unsigned)code_start_addr
+ offset
);
666 /* If it's within the old_code object then it must be an
667 * absolute fixup (relative ones are not saved) */
668 if ((old_value
>=(unsigned)old_code
)
669 && (old_value
<((unsigned)old_code
+ nwords
*4)))
670 /* So add the dispacement. */
671 *(unsigned *)((unsigned)code_start_addr
+ offset
) = old_value
674 /* It is outside the old code object so it must be a relative
675 * fixup (absolute fixups are not saved). So subtract the
677 *(unsigned *)((unsigned)code_start_addr
+ offset
) = old_value
682 /* No longer need the fixups. */
683 new_code
->constants
[0] = 0;
686 /* Check for possible errors. */
687 sniff_code_object(new_code
,displacement
);
693 ptrans_code(lispobj thing
)
695 struct code
*code
, *new;
697 lispobj func
, result
;
699 code
= (struct code
*)native_pointer(thing
);
700 nwords
= HeaderValue(code
->header
) + fixnum_value(code
->code_size
);
702 new = (struct code
*)read_only_free
;
703 read_only_free
+= CEILING(nwords
, 2);
705 bcopy(code
, new, nwords
* sizeof(lispobj
));
708 apply_code_fixups_during_purify(code
,new);
711 result
= (lispobj
)new | OTHER_POINTER_LOWTAG
;
713 /* Stick in a forwarding pointer for the code object. */
714 *(lispobj
*)code
= result
;
716 /* Put in forwarding pointers for all the functions. */
717 for (func
= code
->entry_points
;
719 func
= ((struct simple_fun
*)native_pointer(func
))->next
) {
721 gc_assert(lowtag_of(func
) == FUN_POINTER_LOWTAG
);
723 *(lispobj
*)native_pointer(func
) = result
+ (func
- thing
);
726 /* Arrange to scavenge the debug info later. */
727 pscav_later(&new->debug_info
, 1);
729 if (new->trace_table_offset
& 0x3)
731 pscav(&new->trace_table_offset
, 1, 0);
733 new->trace_table_offset
= NIL
; /* limit lifetime */
736 /* Scavenge the constants. */
737 pscav(new->constants
, HeaderValue(new->header
)-5, 1);
739 /* Scavenge all the functions. */
740 pscav(&new->entry_points
, 1, 1);
741 for (func
= new->entry_points
;
743 func
= ((struct simple_fun
*)native_pointer(func
))->next
) {
744 gc_assert(lowtag_of(func
) == FUN_POINTER_LOWTAG
);
745 gc_assert(!dynamic_pointer_p(func
));
748 /* Temporarly convert the self pointer to a real function pointer. */
749 ((struct simple_fun
*)native_pointer(func
))->self
750 -= FUN_RAW_ADDR_OFFSET
;
752 pscav(&((struct simple_fun
*)native_pointer(func
))->self
, 2, 1);
754 ((struct simple_fun
*)native_pointer(func
))->self
755 += FUN_RAW_ADDR_OFFSET
;
757 pscav_later(&((struct simple_fun
*)native_pointer(func
))->name
, 3);
764 ptrans_func(lispobj thing
, lispobj header
)
767 lispobj code
, *new, *old
, result
;
768 struct simple_fun
*function
;
770 /* Thing can either be a function header, a closure function
771 * header, a closure, or a funcallable-instance. If it's a closure
772 * or a funcallable-instance, we do the same as ptrans_boxed.
773 * Otherwise we have to do something strange, 'cause it is buried
774 * inside a code object. */
776 if (widetag_of(header
) == SIMPLE_FUN_HEADER_WIDETAG
||
777 widetag_of(header
) == CLOSURE_FUN_HEADER_WIDETAG
) {
779 /* We can only end up here if the code object has not been
780 * scavenged, because if it had been scavenged, forwarding pointers
781 * would have been left behind for all the entry points. */
783 function
= (struct simple_fun
*)native_pointer(thing
);
785 (native_pointer(thing
) -
786 (HeaderValue(function
->header
)*sizeof(lispobj
))) |
787 OTHER_POINTER_LOWTAG
;
789 /* This will cause the function's header to be replaced with a
790 * forwarding pointer. */
793 /* So we can just return that. */
794 return function
->header
;
797 /* It's some kind of closure-like thing. */
798 nwords
= 1 + HeaderValue(header
);
799 old
= (lispobj
*)native_pointer(thing
);
801 /* Allocate the new one. */
802 if (widetag_of(header
) == FUNCALLABLE_INSTANCE_HEADER_WIDETAG
) {
803 /* FINs *must* not go in read_only space. */
805 static_free
+= CEILING(nwords
, 2);
808 /* Closures can always go in read-only space, 'cause they
811 new = read_only_free
;
812 read_only_free
+= CEILING(nwords
, 2);
815 bcopy(old
, new, nwords
* sizeof(lispobj
));
817 /* Deposit forwarding pointer. */
818 result
= (lispobj
)new | lowtag_of(thing
);
822 pscav(new, nwords
, 0);
829 ptrans_returnpc(lispobj thing
, lispobj header
)
833 /* Find the corresponding code object. */
834 code
= thing
- HeaderValue(header
)*sizeof(lispobj
);
836 /* Make sure it's been transported. */
837 new = *(lispobj
*)native_pointer(code
);
838 if (!forwarding_pointer_p(new))
839 new = ptrans_code(code
);
841 /* Maintain the offset: */
842 return new + (thing
- code
);
845 #define WORDS_PER_CONS CEILING(sizeof(struct cons) / sizeof(lispobj), 2)
848 ptrans_list(lispobj thing
, boolean constant
)
850 struct cons
*old
, *new, *orig
;
854 orig
= (struct cons
*)read_only_free
;
856 orig
= (struct cons
*)static_free
;
860 /* Allocate a new cons cell. */
861 old
= (struct cons
*)native_pointer(thing
);
863 new = (struct cons
*)read_only_free
;
864 read_only_free
+= WORDS_PER_CONS
;
867 new = (struct cons
*)static_free
;
868 static_free
+= WORDS_PER_CONS
;
871 /* Copy the cons cell and keep a pointer to the cdr. */
873 thing
= new->cdr
= old
->cdr
;
875 /* Set up the forwarding pointer. */
876 *(lispobj
*)old
= ((lispobj
)new) | LIST_POINTER_LOWTAG
;
878 /* And count this cell. */
880 } while (lowtag_of(thing
) == LIST_POINTER_LOWTAG
&&
881 dynamic_pointer_p(thing
) &&
882 !(forwarding_pointer_p(*(lispobj
*)native_pointer(thing
))));
884 /* Scavenge the list we just copied. */
885 pscav((lispobj
*)orig
, length
* WORDS_PER_CONS
, constant
);
887 return ((lispobj
)orig
) | LIST_POINTER_LOWTAG
;
891 ptrans_otherptr(lispobj thing
, lispobj header
, boolean constant
)
893 switch (widetag_of(header
)) {
895 case SINGLE_FLOAT_WIDETAG
:
896 case DOUBLE_FLOAT_WIDETAG
:
897 #ifdef LONG_FLOAT_WIDETAG
898 case LONG_FLOAT_WIDETAG
:
900 #ifdef COMPLEX_SINGLE_FLOAT_WIDETAG
901 case COMPLEX_SINGLE_FLOAT_WIDETAG
:
903 #ifdef COMPLEX_DOUBLE_FLOAT_WIDETAG
904 case COMPLEX_DOUBLE_FLOAT_WIDETAG
:
906 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
907 case COMPLEX_LONG_FLOAT_WIDETAG
:
910 return ptrans_unboxed(thing
, header
);
913 case COMPLEX_WIDETAG
:
914 case SIMPLE_ARRAY_WIDETAG
:
915 case COMPLEX_STRING_WIDETAG
:
916 case COMPLEX_VECTOR_WIDETAG
:
917 case COMPLEX_ARRAY_WIDETAG
:
918 return ptrans_boxed(thing
, header
, constant
);
920 case VALUE_CELL_HEADER_WIDETAG
:
921 case WEAK_POINTER_WIDETAG
:
922 return ptrans_boxed(thing
, header
, 0);
924 case SYMBOL_HEADER_WIDETAG
:
925 return ptrans_boxed(thing
, header
, 0);
927 case SIMPLE_STRING_WIDETAG
:
928 return ptrans_vector(thing
, 8, 1, 0, constant
);
930 case SIMPLE_BIT_VECTOR_WIDETAG
:
931 return ptrans_vector(thing
, 1, 0, 0, constant
);
933 case SIMPLE_VECTOR_WIDETAG
:
934 return ptrans_vector(thing
, 32, 0, 1, constant
);
936 case SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG
:
937 return ptrans_vector(thing
, 2, 0, 0, constant
);
939 case SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG
:
940 return ptrans_vector(thing
, 4, 0, 0, constant
);
942 case SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG
:
943 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
944 case SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
:
946 return ptrans_vector(thing
, 8, 0, 0, constant
);
948 case SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG
:
949 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
950 case SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
:
952 return ptrans_vector(thing
, 16, 0, 0, constant
);
954 case SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG
:
955 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG
956 case SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG
:
958 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
959 case SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
:
961 return ptrans_vector(thing
, 32, 0, 0, constant
);
963 case SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG
:
964 return ptrans_vector(thing
, 32, 0, 0, constant
);
966 case SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG
:
967 return ptrans_vector(thing
, 64, 0, 0, constant
);
969 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
970 case SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
:
972 return ptrans_vector(thing
, 96, 0, 0, constant
);
975 return ptrans_vector(thing
, 128, 0, 0, constant
);
979 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
980 case SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
:
981 return ptrans_vector(thing
, 64, 0, 0, constant
);
984 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
985 case SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
:
986 return ptrans_vector(thing
, 128, 0, 0, constant
);
989 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
990 case SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
:
992 return ptrans_vector(thing
, 192, 0, 0, constant
);
995 return ptrans_vector(thing
, 256, 0, 0, constant
);
999 case CODE_HEADER_WIDETAG
:
1000 return ptrans_code(thing
);
1002 case RETURN_PC_HEADER_WIDETAG
:
1003 return ptrans_returnpc(thing
, header
);
1006 return ptrans_fdefn(thing
, header
);
1009 /* Should only come across other pointers to the above stuff. */
1016 pscav_fdefn(struct fdefn
*fdefn
)
1020 fix_func
= ((char *)(fdefn
->fun
+FUN_RAW_ADDR_OFFSET
) == fdefn
->raw_addr
);
1021 pscav(&fdefn
->name
, 1, 1);
1022 pscav(&fdefn
->fun
, 1, 0);
1024 fdefn
->raw_addr
= (char *)(fdefn
->fun
+ FUN_RAW_ADDR_OFFSET
);
1025 return sizeof(struct fdefn
) / sizeof(lispobj
);
1029 /* now putting code objects in static space */
1031 pscav_code(struct code
*code
)
1035 nwords
= HeaderValue(code
->header
) + fixnum_value(code
->code_size
);
1037 /* Arrange to scavenge the debug info later. */
1038 pscav_later(&code
->debug_info
, 1);
1040 /* Scavenge the constants. */
1041 pscav(code
->constants
, HeaderValue(code
->header
)-5, 1);
1043 /* Scavenge all the functions. */
1044 pscav(&code
->entry_points
, 1, 1);
1045 for (func
= code
->entry_points
;
1047 func
= ((struct simple_fun
*)native_pointer(func
))->next
) {
1048 gc_assert(lowtag_of(func
) == FUN_POINTER_LOWTAG
);
1049 gc_assert(!dynamic_pointer_p(func
));
1052 /* Temporarly convert the self pointer to a real function
1054 ((struct simple_fun
*)native_pointer(func
))->self
1055 -= FUN_RAW_ADDR_OFFSET
;
1057 pscav(&((struct simple_fun
*)native_pointer(func
))->self
, 2, 1);
1059 ((struct simple_fun
*)native_pointer(func
))->self
1060 += FUN_RAW_ADDR_OFFSET
;
1062 pscav_later(&((struct simple_fun
*)native_pointer(func
))->name
, 3);
1065 return CEILING(nwords
,2);
1070 pscav(lispobj
*addr
, int nwords
, boolean constant
)
1072 lispobj thing
, *thingp
, header
;
1073 int count
= 0; /* (0 = dummy init value to stop GCC warning) */
1074 struct vector
*vector
;
1076 while (nwords
> 0) {
1078 if (is_lisp_pointer(thing
)) {
1079 /* It's a pointer. Is it something we might have to move? */
1080 if (dynamic_pointer_p(thing
)) {
1081 /* Maybe. Have we already moved it? */
1082 thingp
= (lispobj
*)native_pointer(thing
);
1084 if (is_lisp_pointer(header
) && forwarding_pointer_p(header
))
1085 /* Yep, so just copy the forwarding pointer. */
1088 /* Nope, copy the object. */
1089 switch (lowtag_of(thing
)) {
1090 case FUN_POINTER_LOWTAG
:
1091 thing
= ptrans_func(thing
, header
);
1094 case LIST_POINTER_LOWTAG
:
1095 thing
= ptrans_list(thing
, constant
);
1098 case INSTANCE_POINTER_LOWTAG
:
1099 thing
= ptrans_instance(thing
, header
, constant
);
1102 case OTHER_POINTER_LOWTAG
:
1103 thing
= ptrans_otherptr(thing
, header
, constant
);
1107 /* It was a pointer, but not one of them? */
1115 else if (thing
& 3) {
1116 /* It's an other immediate. Maybe the header for an unboxed */
1118 switch (widetag_of(thing
)) {
1119 case BIGNUM_WIDETAG
:
1120 case SINGLE_FLOAT_WIDETAG
:
1121 case DOUBLE_FLOAT_WIDETAG
:
1122 #ifdef LONG_FLOAT_WIDETAG
1123 case LONG_FLOAT_WIDETAG
:
1126 /* It's an unboxed simple object. */
1127 count
= HeaderValue(thing
)+1;
1130 case SIMPLE_VECTOR_WIDETAG
:
1131 if (HeaderValue(thing
) == subtype_VectorValidHashing
) {
1132 *addr
= (subtype_VectorMustRehash
<< N_WIDETAG_BITS
) |
1133 SIMPLE_VECTOR_WIDETAG
;
1138 case SIMPLE_STRING_WIDETAG
:
1139 vector
= (struct vector
*)addr
;
1140 count
= CEILING(NWORDS(fixnum_value(vector
->length
)+1,4)+2,2);
1143 case SIMPLE_BIT_VECTOR_WIDETAG
:
1144 vector
= (struct vector
*)addr
;
1145 count
= CEILING(NWORDS(fixnum_value(vector
->length
),32)+2,2);
1148 case SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG
:
1149 vector
= (struct vector
*)addr
;
1150 count
= CEILING(NWORDS(fixnum_value(vector
->length
),16)+2,2);
1153 case SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG
:
1154 vector
= (struct vector
*)addr
;
1155 count
= CEILING(NWORDS(fixnum_value(vector
->length
),8)+2,2);
1158 case SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG
:
1159 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
1160 case SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
:
1162 vector
= (struct vector
*)addr
;
1163 count
= CEILING(NWORDS(fixnum_value(vector
->length
),4)+2,2);
1166 case SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG
:
1167 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
1168 case SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
:
1170 vector
= (struct vector
*)addr
;
1171 count
= CEILING(NWORDS(fixnum_value(vector
->length
),2)+2,2);
1174 case SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG
:
1175 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG
1176 case SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG
:
1178 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
1179 case SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
:
1181 vector
= (struct vector
*)addr
;
1182 count
= CEILING(fixnum_value(vector
->length
)+2,2);
1185 case SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG
:
1186 vector
= (struct vector
*)addr
;
1187 count
= CEILING(fixnum_value(vector
->length
)+2,2);
1190 case SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG
:
1191 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
1192 case SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
:
1194 vector
= (struct vector
*)addr
;
1195 count
= fixnum_value(vector
->length
)*2+2;
1198 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
1199 case SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
:
1200 vector
= (struct vector
*)addr
;
1202 count
= fixnum_value(vector
->length
)*3+2;
1205 count
= fixnum_value(vector
->length
)*4+2;
1210 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
1211 case SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
:
1212 vector
= (struct vector
*)addr
;
1213 count
= fixnum_value(vector
->length
)*4+2;
1217 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
1218 case SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
:
1219 vector
= (struct vector
*)addr
;
1221 count
= fixnum_value(vector
->length
)*6+2;
1224 count
= fixnum_value(vector
->length
)*8+2;
1229 case CODE_HEADER_WIDETAG
:
1231 gc_abort(); /* no code headers in static space */
1233 count
= pscav_code((struct code
*)addr
);
1237 case SIMPLE_FUN_HEADER_WIDETAG
:
1238 case CLOSURE_FUN_HEADER_WIDETAG
:
1239 case RETURN_PC_HEADER_WIDETAG
:
1240 /* We should never hit any of these, 'cause they occur
1241 * buried in the middle of code objects. */
1246 case CLOSURE_HEADER_WIDETAG
:
1247 case FUNCALLABLE_INSTANCE_HEADER_WIDETAG
:
1248 /* The function self pointer needs special care on the
1249 * x86 because it is the real entry point. */
1251 lispobj fun
= ((struct closure
*)addr
)->fun
1252 - FUN_RAW_ADDR_OFFSET
;
1253 pscav(&fun
, 1, constant
);
1254 ((struct closure
*)addr
)->fun
= fun
+ FUN_RAW_ADDR_OFFSET
;
1260 case WEAK_POINTER_WIDETAG
:
1261 /* Weak pointers get preserved during purify, 'cause I
1262 * don't feel like figuring out how to break them. */
1263 pscav(addr
+1, 2, constant
);
1268 /* We have to handle fdefn objects specially, so we
1269 * can fix up the raw function address. */
1270 count
= pscav_fdefn((struct fdefn
*)addr
);
1279 /* It's a fixnum. */
1291 purify(lispobj static_roots
, lispobj read_only_roots
)
1295 struct later
*laters
, *next
;
1298 printf("[doing purification:");
1302 if (fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX
)) != 0) {
1303 /* FIXME: 1. What does this mean? 2. It shouldn't be reporting
1304 * its error simply by a. printing a string b. to stdout instead
1306 printf(" Ack! Can't purify interrupt contexts. ");
1311 #if defined(__i386__)
1312 dynamic_space_free_pointer
=
1313 (lispobj
*)SymbolValue(ALLOCATION_POINTER
);
1316 read_only_end
= read_only_free
=
1317 (lispobj
*)SymbolValue(READ_ONLY_SPACE_FREE_POINTER
);
1318 static_end
= static_free
=
1319 (lispobj
*)SymbolValue(STATIC_SPACE_FREE_POINTER
);
1327 gc_assert((lispobj
*)CONTROL_STACK_END
> ((&read_only_roots
)+1));
1328 setup_i386_stack_scav(((&static_roots
)-2), (lispobj
*)CONTROL_STACK_END
);
1331 pscav(&static_roots
, 1, 0);
1332 pscav(&read_only_roots
, 1, 1);
1335 printf(" handlers");
1338 pscav((lispobj
*) interrupt_handlers
,
1339 sizeof(interrupt_handlers
) / sizeof(lispobj
),
1347 pscav((lispobj
*)CONTROL_STACK_START
,
1348 current_control_stack_pointer
- (lispobj
*)CONTROL_STACK_START
,
1357 printf(" bindings");
1360 #if !defined(__i386__)
1361 pscav( (lispobj
*)BINDING_STACK_START
,
1362 (lispobj
*)current_binding_stack_pointer
- (lispobj
*)BINDING_STACK_START
,
1365 pscav( (lispobj
*)BINDING_STACK_START
,
1366 (lispobj
*)SymbolValue(BINDING_STACK_POINTER
) -
1367 (lispobj
*)BINDING_STACK_START
,
1371 /* The original CMU CL code had scavenge-read-only-space code
1372 * controlled by the Lisp-level variable
1373 * *SCAVENGE-READ-ONLY-SPACE*. It was disabled by default, and it
1374 * wasn't documented under what circumstances it was useful or
1375 * safe to turn it on, so it's been turned off in SBCL. If you
1376 * want/need this functionality, and can test and document it,
1377 * please submit a patch. */
1379 if (SymbolValue(SCAVENGE_READ_ONLY_SPACE
) != UNBOUND_MARKER_WIDETAG
1380 && SymbolValue(SCAVENGE_READ_ONLY_SPACE
) != NIL
) {
1381 unsigned read_only_space_size
=
1382 (lispobj
*)SymbolValue(READ_ONLY_SPACE_FREE_POINTER
) -
1383 (lispobj
*)READ_ONLY_SPACE_START
;
1385 "scavenging read only space: %d bytes\n",
1386 read_only_space_size
* sizeof(lispobj
));
1387 pscav( (lispobj
*)READ_ONLY_SPACE_START
, read_only_space_size
, 0);
1395 clean
= (lispobj
*)STATIC_SPACE_START
;
1397 while (clean
!= static_free
)
1398 clean
= pscav(clean
, static_free
- clean
, 0);
1399 laters
= later_blocks
;
1400 count
= later_count
;
1401 later_blocks
= NULL
;
1403 while (laters
!= NULL
) {
1404 for (i
= 0; i
< count
; i
++) {
1405 if (laters
->u
[i
].count
== 0) {
1407 } else if (laters
->u
[i
].count
<= LATERMAXCOUNT
) {
1408 pscav(laters
->u
[i
+1].ptr
, laters
->u
[i
].count
, 1);
1411 pscav(laters
->u
[i
].ptr
, 1, 1);
1414 next
= laters
->next
;
1417 count
= LATERBLOCKSIZE
;
1419 } while (clean
!= static_free
|| later_blocks
!= NULL
);
1426 os_zero((os_vm_address_t
) current_dynamic_space
,
1427 (os_vm_size_t
) DYNAMIC_SPACE_SIZE
);
1429 /* Zero the stack. Note that the stack is also zeroed by SUB-GC
1430 * calling SCRUB-CONTROL-STACK - this zeros the stack on the x86. */
1432 os_zero((os_vm_address_t
) current_control_stack_pointer
,
1433 (os_vm_size_t
) (CONTROL_STACK_SIZE
-
1434 ((current_control_stack_pointer
-
1435 (lispobj
*)CONTROL_STACK_START
) *
1439 /* It helps to update the heap free pointers so that free_heap can
1440 * verify after it's done. */
1441 SetSymbolValue(READ_ONLY_SPACE_FREE_POINTER
, (lispobj
)read_only_free
);
1442 SetSymbolValue(STATIC_SPACE_FREE_POINTER
, (lispobj
)static_free
);
1444 #if !defined(__i386__)
1445 dynamic_space_free_pointer
= current_dynamic_space
;
1450 #error unsupported case /* in CMU CL, was "ibmrt using GC" */