2 * C-level stuff to implement Lisp-level PURIFY
6 * This software is part of the SBCL system. See the README file for
9 * This software is derived from the CMU CL system, which was
10 * written at Carnegie Mellon University and released into the
11 * public domain. The software is in the public domain and is
12 * provided with absolutely no warranty. See the COPYING and CREDITS
13 * files for more information.
17 #include <sys/types.h>
27 #include "interrupt.h"
32 #include "gc-internal.h"
34 #include "genesis/primitive-objects.h"
35 #include "genesis/static-symbols.h"
36 #include "genesis/layout.h"
40 #if defined(LISP_FEATURE_GENCGC)
41 /* this is another artifact of the poor integration between gencgc and
42 * the rest of the runtime: on cheney gc there is a global
43 * dynamic_space_free_pointer which is valid whenever foreign function
44 * call is active, but in gencgc there's no such variable and we have
47 static lispobj *dynamic_space_free_pointer;
49 extern unsigned long bytes_consed_between_gcs;
52 lose("GC invariant lost, file \"%s\", line %d", __FILE__, __LINE__)
55 #define gc_assert(ex) do { \
56 if (!(ex)) gc_abort(); \
63 /* These hold the original end of the read_only and static spaces so
64 * we can tell what are forwarding pointers. */
66 static lispobj *read_only_end, *static_end;
68 static lispobj *read_only_free, *static_free;
70 static lispobj *pscav(lispobj *addr, long nwords, boolean constant);
72 #define LATERBLOCKSIZE 1020
73 #define LATERMAXCOUNT 10
82 } *later_blocks = NULL;
83 static long later_count = 0;
86 #define SIMPLE_ARRAY_WORD_WIDETAG SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG
87 #elif N_WORD_BITS == 64
88 #define SIMPLE_ARRAY_WORD_WIDETAG SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG
91 /* FIXME: Shouldn't this be defined in sbcl.h? See also notes in
94 #ifdef LISP_FEATURE_SPARC
95 #define FUN_RAW_ADDR_OFFSET 0
97 #define FUN_RAW_ADDR_OFFSET (6*sizeof(lispobj) - FUN_POINTER_LOWTAG)
101 forwarding_pointer_p(lispobj obj)
103 lispobj *ptr = native_pointer(obj);
105 return ((static_end <= ptr && ptr <= static_free) ||
106 (read_only_end <= ptr && ptr <= read_only_free));
110 dynamic_pointer_p(lispobj ptr)
112 #ifndef LISP_FEATURE_GENCGC
113 return (ptr >= (lispobj)current_dynamic_space
115 ptr < (lispobj)dynamic_space_free_pointer);
117 /* Be more conservative, and remember, this is a maybe. */
118 return (ptr >= (lispobj)DYNAMIC_SPACE_START
120 ptr < (lispobj)dynamic_space_free_pointer);
124 static inline lispobj *
125 newspace_alloc(long nwords, int constantp)
128 nwords=CEILING(nwords,2);
131 read_only_free+=nwords;
141 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
143 #ifdef LISP_FEATURE_GENCGC
145 * enhanced x86/GENCGC stack scavenging by Douglas Crosher
147 * Scavenging the stack on the i386 is problematic due to conservative
148 * roots and raw return addresses. Here it is handled in two passes:
149 * the first pass runs before any objects are moved and tries to
150 * identify valid pointers and return address on the stack, the second
151 * pass scavenges these.
154 static unsigned pointer_filter_verbose = 0;
156 /* FIXME: This is substantially the same code as
157 * possibly_valid_dynamic_space_pointer in gencgc.c. The only
158 * relevant difference seems to be that the gencgc code also checks
159 * for raw pointers into Code objects, whereas in purify these are
160 * checked separately in setup_i386_stack_scav - they go onto
161 * valid_stack_ra_locations instead of just valid_stack_locations */
164 valid_dynamic_space_pointer(lispobj *pointer, lispobj *start_addr)
166 /* If it's not a return address then it needs to be a valid Lisp
168 if (!is_lisp_pointer((lispobj)pointer))
171 /* Check that the object pointed to is consistent with the pointer
173 switch (lowtag_of((lispobj)pointer)) {
174 case FUN_POINTER_LOWTAG:
175 /* Start_addr should be the enclosing code object, or a closure
177 switch (widetag_of(*start_addr)) {
178 case CODE_HEADER_WIDETAG:
179 /* This case is probably caught above. */
181 case CLOSURE_HEADER_WIDETAG:
182 case FUNCALLABLE_INSTANCE_HEADER_WIDETAG:
183 if ((long)pointer != ((long)start_addr+FUN_POINTER_LOWTAG)) {
184 if (pointer_filter_verbose) {
185 fprintf(stderr,"*Wf2: %x %x %x\n",
186 (unsigned long) pointer,
187 (unsigned long) start_addr, *start_addr);
193 if (pointer_filter_verbose) {
194 fprintf(stderr,"*Wf3: %x %x %x\n", (unsigned long) pointer,
195 (unsigned long) start_addr, *start_addr);
200 case LIST_POINTER_LOWTAG:
201 if ((long)pointer != ((long)start_addr+LIST_POINTER_LOWTAG)) {
202 if (pointer_filter_verbose)
203 fprintf(stderr,"*Wl1: %x %x %x\n", (unsigned long) pointer,
204 (unsigned long) start_addr, *start_addr);
207 /* Is it plausible cons? */
208 if ((is_lisp_pointer(start_addr[0])
209 || ((start_addr[0] & FIXNUM_TAG_MASK) == 0) /* fixnum */
210 || (widetag_of(start_addr[0]) == CHARACTER_WIDETAG)
211 #if N_WORD_BITS == 64
212 || (widetag_of(start_addr[0]) == SINGLE_FLOAT_WIDETAG)
214 || (widetag_of(start_addr[0]) == UNBOUND_MARKER_WIDETAG))
215 && (is_lisp_pointer(start_addr[1])
216 || ((start_addr[1] & FIXNUM_TAG_MASK) == 0) /* fixnum */
217 || (widetag_of(start_addr[1]) == CHARACTER_WIDETAG)
218 #if N_WORD_BITS == 64
219 || (widetag_of(start_addr[1]) == SINGLE_FLOAT_WIDETAG)
221 || (widetag_of(start_addr[1]) == UNBOUND_MARKER_WIDETAG))) {
224 if (pointer_filter_verbose) {
225 fprintf(stderr,"*Wl2: %x %x %x\n", (unsigned long) pointer,
226 (unsigned long) start_addr, *start_addr);
230 case INSTANCE_POINTER_LOWTAG:
231 if ((long)pointer != ((long)start_addr+INSTANCE_POINTER_LOWTAG)) {
232 if (pointer_filter_verbose) {
233 fprintf(stderr,"*Wi1: %x %x %x\n", (unsigned long) pointer,
234 (unsigned long) start_addr, *start_addr);
238 if (widetag_of(start_addr[0]) != INSTANCE_HEADER_WIDETAG) {
239 if (pointer_filter_verbose) {
240 fprintf(stderr,"*Wi2: %x %x %x\n", (unsigned long) pointer,
241 (unsigned long) start_addr, *start_addr);
246 case OTHER_POINTER_LOWTAG:
247 if ((long)pointer != ((long)start_addr+OTHER_POINTER_LOWTAG)) {
248 if (pointer_filter_verbose) {
249 fprintf(stderr,"*Wo1: %x %x %x\n", (unsigned long) pointer,
250 (unsigned long) start_addr, *start_addr);
254 /* Is it plausible? Not a cons. XXX should check the headers. */
255 if (is_lisp_pointer(start_addr[0]) || ((start_addr[0] & FIXNUM_TAG_MASK) == 0)) {
256 if (pointer_filter_verbose) {
257 fprintf(stderr,"*Wo2: %x %x %x\n", (unsigned long) pointer,
258 (unsigned long) start_addr, *start_addr);
262 switch (widetag_of(start_addr[0])) {
263 case UNBOUND_MARKER_WIDETAG:
264 case CHARACTER_WIDETAG:
265 #if N_WORD_BITS == 64
266 case SINGLE_FLOAT_WIDETAG:
268 if (pointer_filter_verbose) {
269 fprintf(stderr,"*Wo3: %x %x %x\n", (unsigned long) pointer,
270 (unsigned long) start_addr, *start_addr);
274 /* only pointed to by function pointers? */
275 case CLOSURE_HEADER_WIDETAG:
276 case FUNCALLABLE_INSTANCE_HEADER_WIDETAG:
277 if (pointer_filter_verbose) {
278 fprintf(stderr,"*Wo4: %x %x %x\n", (unsigned long) pointer,
279 (unsigned long) start_addr, *start_addr);
283 case INSTANCE_HEADER_WIDETAG:
284 if (pointer_filter_verbose) {
285 fprintf(stderr,"*Wo5: %x %x %x\n", (unsigned long) pointer,
286 (unsigned long) start_addr, *start_addr);
290 /* the valid other immediate pointer objects */
291 case SIMPLE_VECTOR_WIDETAG:
293 case COMPLEX_WIDETAG:
294 #ifdef COMPLEX_SINGLE_FLOAT_WIDETAG
295 case COMPLEX_SINGLE_FLOAT_WIDETAG:
297 #ifdef COMPLEX_DOUBLE_FLOAT_WIDETAG
298 case COMPLEX_DOUBLE_FLOAT_WIDETAG:
300 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
301 case COMPLEX_LONG_FLOAT_WIDETAG:
303 case SIMPLE_ARRAY_WIDETAG:
304 case COMPLEX_BASE_STRING_WIDETAG:
305 #ifdef COMPLEX_CHARACTER_STRING_WIDETAG
306 case COMPLEX_CHARACTER_STRING_WIDETAG:
308 case COMPLEX_VECTOR_NIL_WIDETAG:
309 case COMPLEX_BIT_VECTOR_WIDETAG:
310 case COMPLEX_VECTOR_WIDETAG:
311 case COMPLEX_ARRAY_WIDETAG:
312 case VALUE_CELL_HEADER_WIDETAG:
313 case SYMBOL_HEADER_WIDETAG:
315 case CODE_HEADER_WIDETAG:
317 #if N_WORD_BITS != 64
318 case SINGLE_FLOAT_WIDETAG:
320 case DOUBLE_FLOAT_WIDETAG:
321 #ifdef LONG_FLOAT_WIDETAG
322 case LONG_FLOAT_WIDETAG:
324 case SIMPLE_ARRAY_NIL_WIDETAG:
325 case SIMPLE_BASE_STRING_WIDETAG:
326 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
327 case SIMPLE_CHARACTER_STRING_WIDETAG:
329 case SIMPLE_BIT_VECTOR_WIDETAG:
330 case SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG:
331 case SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG:
332 case SIMPLE_ARRAY_UNSIGNED_BYTE_7_WIDETAG:
333 case SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG:
334 case SIMPLE_ARRAY_UNSIGNED_BYTE_15_WIDETAG:
335 case SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG:
336 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_29_WIDETAG
337 case SIMPLE_ARRAY_UNSIGNED_BYTE_29_WIDETAG:
339 case SIMPLE_ARRAY_UNSIGNED_BYTE_31_WIDETAG:
340 case SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG:
341 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_60_WIDETAG
342 case SIMPLE_ARRAY_UNSIGNED_BYTE_60_WIDETAG:
344 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG
345 case SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG:
347 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG
348 case SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG:
350 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
351 case SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG:
353 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
354 case SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG:
356 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG
357 case SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG:
359 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
360 case SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG:
362 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_61_WIDETAG
363 case SIMPLE_ARRAY_SIGNED_BYTE_61_WIDETAG:
365 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG
366 case SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG:
368 case SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG:
369 case SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG:
370 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
371 case SIMPLE_ARRAY_LONG_FLOAT_WIDETAG:
373 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
374 case SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG:
376 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
377 case SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG:
379 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
380 case SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG:
383 case WEAK_POINTER_WIDETAG:
387 if (pointer_filter_verbose) {
388 fprintf(stderr,"*Wo6: %x %x %x\n", (unsigned long) pointer,
389 (unsigned long) start_addr, *start_addr);
395 if (pointer_filter_verbose) {
396 fprintf(stderr,"*W?: %x %x %x\n", (unsigned long) pointer,
397 (unsigned long) start_addr, *start_addr);
406 #define MAX_STACK_POINTERS 256
407 lispobj *valid_stack_locations[MAX_STACK_POINTERS];
408 unsigned long num_valid_stack_locations;
410 #define MAX_STACK_RETURN_ADDRESSES 128
411 lispobj *valid_stack_ra_locations[MAX_STACK_RETURN_ADDRESSES];
412 lispobj *valid_stack_ra_code_objects[MAX_STACK_RETURN_ADDRESSES];
413 unsigned long num_valid_stack_ra_locations;
415 /* Identify valid stack slots. */
417 setup_i386_stack_scav(lispobj *lowaddr, lispobj *base)
419 lispobj *sp = lowaddr;
420 num_valid_stack_locations = 0;
421 num_valid_stack_ra_locations = 0;
422 for (sp = lowaddr; sp < base; sp++) {
424 /* Find the object start address */
425 lispobj *start_addr = search_dynamic_space((void *)thing);
427 /* We need to allow raw pointers into Code objects for
428 * return addresses. This will also pick up pointers to
429 * functions in code objects. */
430 if (widetag_of(*start_addr) == CODE_HEADER_WIDETAG) {
431 /* FIXME asserting here is a really dumb thing to do.
432 * If we've overflowed some arbitrary static limit, we
433 * should just refuse to purify, instead of killing
434 * the whole lisp session
436 gc_assert(num_valid_stack_ra_locations <
437 MAX_STACK_RETURN_ADDRESSES);
438 valid_stack_ra_locations[num_valid_stack_ra_locations] = sp;
439 valid_stack_ra_code_objects[num_valid_stack_ra_locations++] =
440 (lispobj *)((long)start_addr + OTHER_POINTER_LOWTAG);
442 if (valid_dynamic_space_pointer((void *)thing, start_addr)) {
443 gc_assert(num_valid_stack_locations < MAX_STACK_POINTERS);
444 valid_stack_locations[num_valid_stack_locations++] = sp;
449 if (pointer_filter_verbose) {
450 fprintf(stderr, "number of valid stack pointers = %d\n",
451 num_valid_stack_locations);
452 fprintf(stderr, "number of stack return addresses = %d\n",
453 num_valid_stack_ra_locations);
458 pscav_i386_stack(void)
462 for (i = 0; i < num_valid_stack_locations; i++)
463 pscav(valid_stack_locations[i], 1, 0);
465 for (i = 0; i < num_valid_stack_ra_locations; i++) {
466 lispobj code_obj = (lispobj)valid_stack_ra_code_objects[i];
467 pscav(&code_obj, 1, 0);
468 if (pointer_filter_verbose) {
469 fprintf(stderr,"*C moved RA %x to %x; for code object %x to %x\n",
470 *valid_stack_ra_locations[i],
471 (long)(*valid_stack_ra_locations[i])
472 - ((long)valid_stack_ra_code_objects[i] - (long)code_obj),
473 (unsigned long) valid_stack_ra_code_objects[i], code_obj);
475 *valid_stack_ra_locations[i] =
476 ((long)(*valid_stack_ra_locations[i])
477 - ((long)valid_stack_ra_code_objects[i] - (long)code_obj));
485 pscav_later(lispobj *where, long count)
489 if (count > LATERMAXCOUNT) {
490 while (count > LATERMAXCOUNT) {
491 pscav_later(where, LATERMAXCOUNT);
492 count -= LATERMAXCOUNT;
493 where += LATERMAXCOUNT;
497 if (later_blocks == NULL || later_count == LATERBLOCKSIZE ||
498 (later_count == LATERBLOCKSIZE-1 && count > 1)) {
499 new = (struct later *)malloc(sizeof(struct later));
500 new->next = later_blocks;
501 if (later_blocks && later_count < LATERBLOCKSIZE)
502 later_blocks->u[later_count].ptr = NULL;
508 later_blocks->u[later_count++].count = count;
509 later_blocks->u[later_count++].ptr = where;
514 ptrans_boxed(lispobj thing, lispobj header, boolean constant)
517 lispobj result, *new, *old;
519 nwords = CEILING(1 + HeaderValue(header), 2);
522 old = (lispobj *)native_pointer(thing);
523 new = newspace_alloc(nwords,constant);
526 bcopy(old, new, nwords * sizeof(lispobj));
528 /* Deposit forwarding pointer. */
529 result = make_lispobj(new, lowtag_of(thing));
533 pscav(new, nwords, constant);
538 /* We need to look at the layout to see whether it is a pure structure
539 * class, and only then can we transport as constant. If it is pure,
540 * we can ALWAYS transport as a constant. */
542 ptrans_instance(lispobj thing, lispobj header, boolean /* ignored */ constant)
544 lispobj layout = ((struct instance *)native_pointer(thing))->slots[0];
545 lispobj pure = ((struct instance *)native_pointer(layout))->slots[15];
549 return (ptrans_boxed(thing, header, 1));
551 return (ptrans_boxed(thing, header, 0));
554 /* Substructure: special case for the COMPACT-INFO-ENVs,
555 * where the instance may have a point to the dynamic
556 * space placed into it (e.g. the cache-name slot), but
557 * the lists and arrays at the time of a purify can be
558 * moved to the RO space. */
560 lispobj result, *new, *old;
562 nwords = CEILING(1 + HeaderValue(header), 2);
565 old = (lispobj *)native_pointer(thing);
566 new = newspace_alloc(nwords, 0); /* inconstant */
569 bcopy(old, new, nwords * sizeof(lispobj));
571 /* Deposit forwarding pointer. */
572 result = make_lispobj(new, lowtag_of(thing));
576 pscav(new, nwords, 1);
582 return NIL; /* dummy value: return something ... */
587 ptrans_fdefn(lispobj thing, lispobj header)
590 lispobj result, *new, *old, oldfn;
593 nwords = CEILING(1 + HeaderValue(header), 2);
596 old = (lispobj *)native_pointer(thing);
597 new = newspace_alloc(nwords, 0); /* inconstant */
600 bcopy(old, new, nwords * sizeof(lispobj));
602 /* Deposit forwarding pointer. */
603 result = make_lispobj(new, lowtag_of(thing));
606 /* Scavenge the function. */
607 fdefn = (struct fdefn *)new;
609 pscav(&fdefn->fun, 1, 0);
610 if ((char *)oldfn + FUN_RAW_ADDR_OFFSET == fdefn->raw_addr)
611 fdefn->raw_addr = (char *)fdefn->fun + FUN_RAW_ADDR_OFFSET;
617 ptrans_unboxed(lispobj thing, lispobj header)
620 lispobj result, *new, *old;
622 nwords = CEILING(1 + HeaderValue(header), 2);
625 old = (lispobj *)native_pointer(thing);
626 new = newspace_alloc(nwords,1); /* always constant */
629 bcopy(old, new, nwords * sizeof(lispobj));
631 /* Deposit forwarding pointer. */
632 result = make_lispobj(new , lowtag_of(thing));
639 ptrans_vector(lispobj thing, long bits, long extra,
640 boolean boxed, boolean constant)
642 struct vector *vector;
644 lispobj result, *new;
647 vector = (struct vector *)native_pointer(thing);
648 length = fixnum_value(vector->length)+extra;
649 // Argh, handle simple-vector-nil separately.
653 nwords = CEILING(NWORDS(length, bits) + 2, 2);
656 new=newspace_alloc(nwords, (constant || !boxed));
657 bcopy(vector, new, nwords * sizeof(lispobj));
659 result = make_lispobj(new, lowtag_of(thing));
660 vector->header = result;
663 pscav(new, nwords, constant);
668 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
670 apply_code_fixups_during_purify(struct code *old_code, struct code *new_code)
672 long nheader_words, ncode_words, nwords;
673 void *constants_start_addr, *constants_end_addr;
674 void *code_start_addr, *code_end_addr;
675 lispobj fixups = NIL;
676 unsigned displacement = (unsigned)new_code - (unsigned)old_code;
677 struct vector *fixups_vector;
679 ncode_words = fixnum_value(new_code->code_size);
680 nheader_words = HeaderValue(*(lispobj *)new_code);
681 nwords = ncode_words + nheader_words;
683 constants_start_addr = (void *)new_code + 5 * N_WORD_BYTES;
684 constants_end_addr = (void *)new_code + nheader_words*N_WORD_BYTES;
685 code_start_addr = (void *)new_code + nheader_words*N_WORD_BYTES;
686 code_end_addr = (void *)new_code + nwords*N_WORD_BYTES;
688 /* The first constant should be a pointer to the fixups for this
689 * code objects. Check. */
690 fixups = new_code->constants[0];
692 /* It will be 0 or the unbound-marker if there are no fixups, and
693 * will be an other-pointer to a vector if it is valid. */
695 (fixups==UNBOUND_MARKER_WIDETAG) ||
696 !is_lisp_pointer(fixups)) {
697 #ifdef LISP_FEATURE_GENCGC
698 /* Check for a possible errors. */
699 sniff_code_object(new_code,displacement);
704 fixups_vector = (struct vector *)native_pointer(fixups);
706 /* Could be pointing to a forwarding pointer. */
707 if (is_lisp_pointer(fixups) && (dynamic_pointer_p(fixups))
708 && forwarding_pointer_p(*(lispobj *)fixups_vector)) {
709 /* If so then follow it. */
711 (struct vector *)native_pointer(*(lispobj *)fixups_vector);
714 if (widetag_of(fixups_vector->header) == SIMPLE_ARRAY_WORD_WIDETAG) {
715 /* We got the fixups for the code block. Now work through the
716 * vector, and apply a fixup at each address. */
717 long length = fixnum_value(fixups_vector->length);
719 for (i=0; i<length; i++) {
720 unsigned offset = fixups_vector->data[i];
721 /* Now check the current value of offset. */
723 *(unsigned *)((unsigned)code_start_addr + offset);
725 /* If it's within the old_code object then it must be an
726 * absolute fixup (relative ones are not saved) */
727 if ((old_value>=(unsigned)old_code)
728 && (old_value<((unsigned)old_code + nwords * N_WORD_BYTES)))
729 /* So add the dispacement. */
730 *(unsigned *)((unsigned)code_start_addr + offset) = old_value
733 /* It is outside the old code object so it must be a relative
734 * fixup (absolute fixups are not saved). So subtract the
736 *(unsigned *)((unsigned)code_start_addr + offset) = old_value
741 /* No longer need the fixups. */
742 new_code->constants[0] = 0;
744 #ifdef LISP_FEATURE_GENCGC
745 /* Check for possible errors. */
746 sniff_code_object(new_code,displacement);
752 ptrans_code(lispobj thing)
754 struct code *code, *new;
756 lispobj func, result;
758 code = (struct code *)native_pointer(thing);
759 nwords = CEILING(HeaderValue(code->header) + fixnum_value(code->code_size),
762 new = (struct code *)newspace_alloc(nwords,1); /* constant */
764 bcopy(code, new, nwords * sizeof(lispobj));
766 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
767 apply_code_fixups_during_purify(code,new);
770 result = make_lispobj(new, OTHER_POINTER_LOWTAG);
772 /* Stick in a forwarding pointer for the code object. */
773 *(lispobj *)code = result;
775 /* Put in forwarding pointers for all the functions. */
776 for (func = code->entry_points;
778 func = ((struct simple_fun *)native_pointer(func))->next) {
780 gc_assert(lowtag_of(func) == FUN_POINTER_LOWTAG);
782 *(lispobj *)native_pointer(func) = result + (func - thing);
785 /* Arrange to scavenge the debug info later. */
786 pscav_later(&new->debug_info, 1);
788 /* FIXME: why would this be a fixnum? */
789 /* "why" is a hard word, but apparently for compiled functions the
790 trace_table_offset contains the length of the instructions, as
791 a fixnum. See CODE-INST-AREA-LENGTH in
792 src/compiler/target-disassem.lisp. -- CSR, 2004-01-08 */
793 if (!(fixnump(new->trace_table_offset)))
795 pscav(&new->trace_table_offset, 1, 0);
797 new->trace_table_offset = NIL; /* limit lifetime */
800 /* Scavenge the constants. */
801 pscav(new->constants, HeaderValue(new->header)-5, 1);
803 /* Scavenge all the functions. */
804 pscav(&new->entry_points, 1, 1);
805 for (func = new->entry_points;
807 func = ((struct simple_fun *)native_pointer(func))->next) {
808 gc_assert(lowtag_of(func) == FUN_POINTER_LOWTAG);
809 gc_assert(!dynamic_pointer_p(func));
811 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
812 /* Temporarily convert the self pointer to a real function pointer. */
813 ((struct simple_fun *)native_pointer(func))->self
814 -= FUN_RAW_ADDR_OFFSET;
816 pscav(&((struct simple_fun *)native_pointer(func))->self, 2, 1);
817 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
818 ((struct simple_fun *)native_pointer(func))->self
819 += FUN_RAW_ADDR_OFFSET;
821 pscav_later(&((struct simple_fun *)native_pointer(func))->name, 3);
828 ptrans_func(lispobj thing, lispobj header)
831 lispobj code, *new, *old, result;
832 struct simple_fun *function;
834 /* Thing can either be a function header, a closure function
835 * header, a closure, or a funcallable-instance. If it's a closure
836 * or a funcallable-instance, we do the same as ptrans_boxed.
837 * Otherwise we have to do something strange, 'cause it is buried
838 * inside a code object. */
840 if (widetag_of(header) == SIMPLE_FUN_HEADER_WIDETAG) {
842 /* We can only end up here if the code object has not been
843 * scavenged, because if it had been scavenged, forwarding pointers
844 * would have been left behind for all the entry points. */
846 function = (struct simple_fun *)native_pointer(thing);
849 ((native_pointer(thing) -
850 (HeaderValue(function->header))), OTHER_POINTER_LOWTAG);
852 /* This will cause the function's header to be replaced with a
853 * forwarding pointer. */
857 /* So we can just return that. */
858 return function->header;
861 /* It's some kind of closure-like thing. */
862 nwords = CEILING(1 + HeaderValue(header), 2);
863 old = (lispobj *)native_pointer(thing);
865 /* Allocate the new one. FINs *must* not go in read_only
866 * space. Closures can; they never change */
869 (nwords,(widetag_of(header)!=FUNCALLABLE_INSTANCE_HEADER_WIDETAG));
872 bcopy(old, new, nwords * sizeof(lispobj));
874 /* Deposit forwarding pointer. */
875 result = make_lispobj(new, lowtag_of(thing));
879 pscav(new, nwords, 0);
886 ptrans_returnpc(lispobj thing, lispobj header)
890 /* Find the corresponding code object. */
891 code = thing - HeaderValue(header)*sizeof(lispobj);
893 /* Make sure it's been transported. */
894 new = *(lispobj *)native_pointer(code);
895 if (!forwarding_pointer_p(new))
896 new = ptrans_code(code);
898 /* Maintain the offset: */
899 return new + (thing - code);
902 #define WORDS_PER_CONS CEILING(sizeof(struct cons) / sizeof(lispobj), 2)
905 ptrans_list(lispobj thing, boolean constant)
907 struct cons *old, *new, *orig;
910 orig = (struct cons *) newspace_alloc(0,constant);
914 /* Allocate a new cons cell. */
915 old = (struct cons *)native_pointer(thing);
916 new = (struct cons *) newspace_alloc(WORDS_PER_CONS,constant);
918 /* Copy the cons cell and keep a pointer to the cdr. */
920 thing = new->cdr = old->cdr;
922 /* Set up the forwarding pointer. */
923 *(lispobj *)old = make_lispobj(new, LIST_POINTER_LOWTAG);
925 /* And count this cell. */
927 } while (lowtag_of(thing) == LIST_POINTER_LOWTAG &&
928 dynamic_pointer_p(thing) &&
929 !(forwarding_pointer_p(*(lispobj *)native_pointer(thing))));
931 /* Scavenge the list we just copied. */
932 pscav((lispobj *)orig, length * WORDS_PER_CONS, constant);
934 return make_lispobj(orig, LIST_POINTER_LOWTAG);
938 ptrans_otherptr(lispobj thing, lispobj header, boolean constant)
940 switch (widetag_of(header)) {
941 /* FIXME: this needs a reindent */
943 case SINGLE_FLOAT_WIDETAG:
944 case DOUBLE_FLOAT_WIDETAG:
945 #ifdef LONG_FLOAT_WIDETAG
946 case LONG_FLOAT_WIDETAG:
948 #ifdef COMPLEX_SINGLE_FLOAT_WIDETAG
949 case COMPLEX_SINGLE_FLOAT_WIDETAG:
951 #ifdef COMPLEX_DOUBLE_FLOAT_WIDETAG
952 case COMPLEX_DOUBLE_FLOAT_WIDETAG:
954 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
955 case COMPLEX_LONG_FLOAT_WIDETAG:
958 return ptrans_unboxed(thing, header);
961 case COMPLEX_WIDETAG:
962 case SIMPLE_ARRAY_WIDETAG:
963 case COMPLEX_BASE_STRING_WIDETAG:
964 #ifdef COMPLEX_CHARACTER_STRING_WIDETAG
965 case COMPLEX_CHARACTER_STRING_WIDETAG:
967 case COMPLEX_BIT_VECTOR_WIDETAG:
968 case COMPLEX_VECTOR_NIL_WIDETAG:
969 case COMPLEX_VECTOR_WIDETAG:
970 case COMPLEX_ARRAY_WIDETAG:
971 return ptrans_boxed(thing, header, constant);
973 case VALUE_CELL_HEADER_WIDETAG:
974 case WEAK_POINTER_WIDETAG:
975 return ptrans_boxed(thing, header, 0);
977 case SYMBOL_HEADER_WIDETAG:
978 return ptrans_boxed(thing, header, 0);
980 case SIMPLE_ARRAY_NIL_WIDETAG:
981 return ptrans_vector(thing, 0, 0, 0, constant);
983 case SIMPLE_BASE_STRING_WIDETAG:
984 return ptrans_vector(thing, 8, 1, 0, constant);
986 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
987 case SIMPLE_CHARACTER_STRING_WIDETAG:
988 return ptrans_vector(thing, 32, 1, 0, constant);
991 case SIMPLE_BIT_VECTOR_WIDETAG:
992 return ptrans_vector(thing, 1, 0, 0, constant);
994 case SIMPLE_VECTOR_WIDETAG:
995 return ptrans_vector(thing, N_WORD_BITS, 0, 1, constant);
997 case SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG:
998 return ptrans_vector(thing, 2, 0, 0, constant);
1000 case SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG:
1001 return ptrans_vector(thing, 4, 0, 0, constant);
1003 case SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG:
1004 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
1005 case SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG:
1006 case SIMPLE_ARRAY_UNSIGNED_BYTE_7_WIDETAG:
1008 return ptrans_vector(thing, 8, 0, 0, constant);
1010 case SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG:
1011 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
1012 case SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG:
1013 case SIMPLE_ARRAY_UNSIGNED_BYTE_15_WIDETAG:
1015 return ptrans_vector(thing, 16, 0, 0, constant);
1017 case SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG:
1018 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG
1019 case SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG:
1020 case SIMPLE_ARRAY_UNSIGNED_BYTE_29_WIDETAG:
1022 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
1023 case SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG:
1024 case SIMPLE_ARRAY_UNSIGNED_BYTE_31_WIDETAG:
1026 return ptrans_vector(thing, 32, 0, 0, constant);
1028 #if N_WORD_BITS == 64
1029 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_60_WIDETAG
1030 case SIMPLE_ARRAY_UNSIGNED_BYTE_60_WIDETAG:
1032 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG
1033 case SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG:
1035 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG
1036 case SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG:
1038 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_61_WIDETAG
1039 case SIMPLE_ARRAY_SIGNED_BYTE_61_WIDETAG:
1041 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG
1042 case SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG:
1044 return ptrans_vector(thing, 64, 0, 0, constant);
1047 case SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG:
1048 return ptrans_vector(thing, 32, 0, 0, constant);
1050 case SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG:
1051 return ptrans_vector(thing, 64, 0, 0, constant);
1053 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
1054 case SIMPLE_ARRAY_LONG_FLOAT_WIDETAG:
1055 #ifdef LISP_FEATURE_X86
1056 return ptrans_vector(thing, 96, 0, 0, constant);
1058 #ifdef LISP_FEATURE_SPARC
1059 return ptrans_vector(thing, 128, 0, 0, constant);
1063 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
1064 case SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG:
1065 return ptrans_vector(thing, 64, 0, 0, constant);
1068 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
1069 case SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG:
1070 return ptrans_vector(thing, 128, 0, 0, constant);
1073 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
1074 case SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG:
1075 #ifdef LISP_FEATURE_X86
1076 return ptrans_vector(thing, 192, 0, 0, constant);
1078 #ifdef LISP_FEATURE_SPARC
1079 return ptrans_vector(thing, 256, 0, 0, constant);
1083 case CODE_HEADER_WIDETAG:
1084 return ptrans_code(thing);
1086 case RETURN_PC_HEADER_WIDETAG:
1087 return ptrans_returnpc(thing, header);
1090 return ptrans_fdefn(thing, header);
1093 fprintf(stderr, "Invalid widetag: %d\n", widetag_of(header));
1094 /* Should only come across other pointers to the above stuff. */
1101 pscav_fdefn(struct fdefn *fdefn)
1105 fix_func = ((char *)(fdefn->fun+FUN_RAW_ADDR_OFFSET) == fdefn->raw_addr);
1106 pscav(&fdefn->name, 1, 1);
1107 pscav(&fdefn->fun, 1, 0);
1109 fdefn->raw_addr = (char *)(fdefn->fun + FUN_RAW_ADDR_OFFSET);
1110 return sizeof(struct fdefn) / sizeof(lispobj);
1113 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
1114 /* now putting code objects in static space */
1116 pscav_code(struct code*code)
1120 nwords = CEILING(HeaderValue(code->header) + fixnum_value(code->code_size),
1123 /* Arrange to scavenge the debug info later. */
1124 pscav_later(&code->debug_info, 1);
1126 /* Scavenge the constants. */
1127 pscav(code->constants, HeaderValue(code->header)-5, 1);
1129 /* Scavenge all the functions. */
1130 pscav(&code->entry_points, 1, 1);
1131 for (func = code->entry_points;
1133 func = ((struct simple_fun *)native_pointer(func))->next) {
1134 gc_assert(lowtag_of(func) == FUN_POINTER_LOWTAG);
1135 gc_assert(!dynamic_pointer_p(func));
1137 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
1138 /* Temporarily convert the self pointer to a real function
1140 ((struct simple_fun *)native_pointer(func))->self
1141 -= FUN_RAW_ADDR_OFFSET;
1143 pscav(&((struct simple_fun *)native_pointer(func))->self, 2, 1);
1144 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
1145 ((struct simple_fun *)native_pointer(func))->self
1146 += FUN_RAW_ADDR_OFFSET;
1148 pscav_later(&((struct simple_fun *)native_pointer(func))->name, 3);
1151 return CEILING(nwords,2);
1156 pscav(lispobj *addr, long nwords, boolean constant)
1158 lispobj thing, *thingp, header;
1159 long count = 0; /* (0 = dummy init value to stop GCC warning) */
1160 struct vector *vector;
1162 while (nwords > 0) {
1164 if (is_lisp_pointer(thing)) {
1165 /* It's a pointer. Is it something we might have to move? */
1166 if (dynamic_pointer_p(thing)) {
1167 /* Maybe. Have we already moved it? */
1168 thingp = (lispobj *)native_pointer(thing);
1170 if (is_lisp_pointer(header) && forwarding_pointer_p(header))
1171 /* Yep, so just copy the forwarding pointer. */
1174 /* Nope, copy the object. */
1175 switch (lowtag_of(thing)) {
1176 case FUN_POINTER_LOWTAG:
1177 thing = ptrans_func(thing, header);
1180 case LIST_POINTER_LOWTAG:
1181 thing = ptrans_list(thing, constant);
1184 case INSTANCE_POINTER_LOWTAG:
1185 thing = ptrans_instance(thing, header, constant);
1188 case OTHER_POINTER_LOWTAG:
1189 thing = ptrans_otherptr(thing, header, constant);
1193 /* It was a pointer, but not one of them? */
1201 #if N_WORD_BITS == 64
1202 else if (widetag_of(thing) == SINGLE_FLOAT_WIDETAG) {
1206 else if (thing & FIXNUM_TAG_MASK) {
1207 /* It's an other immediate. Maybe the header for an unboxed */
1209 switch (widetag_of(thing)) {
1210 case BIGNUM_WIDETAG:
1211 case SINGLE_FLOAT_WIDETAG:
1212 case DOUBLE_FLOAT_WIDETAG:
1213 #ifdef LONG_FLOAT_WIDETAG
1214 case LONG_FLOAT_WIDETAG:
1217 /* It's an unboxed simple object. */
1218 count = CEILING(HeaderValue(thing)+1, 2);
1221 case SIMPLE_VECTOR_WIDETAG:
1222 if (HeaderValue(thing) == subtype_VectorValidHashing) {
1223 *addr = (subtype_VectorMustRehash << N_WIDETAG_BITS) |
1224 SIMPLE_VECTOR_WIDETAG;
1229 case SIMPLE_ARRAY_NIL_WIDETAG:
1233 case SIMPLE_BASE_STRING_WIDETAG:
1234 vector = (struct vector *)addr;
1235 count = CEILING(NWORDS(fixnum_value(vector->length)+1,8)+2,2);
1238 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
1239 case SIMPLE_CHARACTER_STRING_WIDETAG:
1240 vector = (struct vector *)addr;
1241 count = CEILING(NWORDS(fixnum_value(vector->length)+1,32)+2,2);
1245 case SIMPLE_BIT_VECTOR_WIDETAG:
1246 vector = (struct vector *)addr;
1247 count = CEILING(NWORDS(fixnum_value(vector->length),1)+2,2);
1250 case SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG:
1251 vector = (struct vector *)addr;
1252 count = CEILING(NWORDS(fixnum_value(vector->length),2)+2,2);
1255 case SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG:
1256 vector = (struct vector *)addr;
1257 count = CEILING(NWORDS(fixnum_value(vector->length),4)+2,2);
1260 case SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG:
1261 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
1262 case SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG:
1263 case SIMPLE_ARRAY_UNSIGNED_BYTE_7_WIDETAG:
1265 vector = (struct vector *)addr;
1266 count = CEILING(NWORDS(fixnum_value(vector->length),8)+2,2);
1269 case SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG:
1270 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
1271 case SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG:
1272 case SIMPLE_ARRAY_UNSIGNED_BYTE_15_WIDETAG:
1274 vector = (struct vector *)addr;
1275 count = CEILING(NWORDS(fixnum_value(vector->length),16)+2,2);
1278 case SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG:
1279 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG
1280 case SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG:
1281 case SIMPLE_ARRAY_UNSIGNED_BYTE_29_WIDETAG:
1283 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
1284 case SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG:
1285 case SIMPLE_ARRAY_UNSIGNED_BYTE_31_WIDETAG:
1287 vector = (struct vector *)addr;
1288 count = CEILING(NWORDS(fixnum_value(vector->length),32)+2,2);
1291 #if N_WORD_BITS == 64
1292 case SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG:
1293 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_61_WIDETAG
1294 case SIMPLE_ARRAY_SIGNED_BYTE_61_WIDETAG:
1295 case SIMPLE_ARRAY_UNSIGNED_BYTE_60_WIDETAG:
1297 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG
1298 case SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG:
1299 case SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG:
1301 vector = (struct vector *)addr;
1302 count = CEILING(NWORDS(fixnum_value(vector->length),64)+2,2);
1306 case SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG:
1307 vector = (struct vector *)addr;
1308 count = CEILING(NWORDS(fixnum_value(vector->length), 32) + 2,
1312 case SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG:
1313 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
1314 case SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG:
1316 vector = (struct vector *)addr;
1317 count = CEILING(NWORDS(fixnum_value(vector->length), 64) + 2,
1321 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
1322 case SIMPLE_ARRAY_LONG_FLOAT_WIDETAG:
1323 vector = (struct vector *)addr;
1324 #ifdef LISP_FEATURE_X86
1325 count = fixnum_value(vector->length)*3+2;
1327 #ifdef LISP_FEATURE_SPARC
1328 count = fixnum_value(vector->length)*4+2;
1333 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
1334 case SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG:
1335 vector = (struct vector *)addr;
1336 count = CEILING(NWORDS(fixnum_value(vector->length), 128) + 2,
1341 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
1342 case SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG:
1343 vector = (struct vector *)addr;
1344 #ifdef LISP_FEATURE_X86
1345 count = fixnum_value(vector->length)*6+2;
1347 #ifdef LISP_FEATURE_SPARC
1348 count = fixnum_value(vector->length)*8+2;
1353 case CODE_HEADER_WIDETAG:
1354 #if !defined(LISP_FEATURE_X86) && !defined(LISP_FEATURE_X86_64)
1355 gc_abort(); /* no code headers in static space */
1357 count = pscav_code((struct code*)addr);
1361 case SIMPLE_FUN_HEADER_WIDETAG:
1362 case RETURN_PC_HEADER_WIDETAG:
1363 /* We should never hit any of these, 'cause they occur
1364 * buried in the middle of code objects. */
1368 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
1369 case CLOSURE_HEADER_WIDETAG:
1370 case FUNCALLABLE_INSTANCE_HEADER_WIDETAG:
1371 /* The function self pointer needs special care on the
1372 * x86 because it is the real entry point. */
1374 lispobj fun = ((struct closure *)addr)->fun
1375 - FUN_RAW_ADDR_OFFSET;
1376 pscav(&fun, 1, constant);
1377 ((struct closure *)addr)->fun = fun + FUN_RAW_ADDR_OFFSET;
1383 case WEAK_POINTER_WIDETAG:
1384 /* Weak pointers get preserved during purify, 'cause I
1385 * don't feel like figuring out how to break them. */
1386 pscav(addr+1, 2, constant);
1391 /* We have to handle fdefn objects specially, so we
1392 * can fix up the raw function address. */
1393 count = pscav_fdefn((struct fdefn *)addr);
1396 case INSTANCE_HEADER_WIDETAG:
1398 struct instance *instance = (struct instance *) addr;
1399 struct layout *layout
1400 = (struct layout *) native_pointer(instance->slots[0]);
1401 long nuntagged = fixnum_value(layout->n_untagged_slots);
1402 long nslots = HeaderValue(*addr);
1403 pscav(addr + 1, nslots - nuntagged, constant);
1404 count = CEILING(1 + nslots, 2);
1414 /* It's a fixnum. */
1426 purify(lispobj static_roots, lispobj read_only_roots)
1430 struct later *laters, *next;
1431 struct thread *thread;
1433 if(all_threads->next) {
1434 /* FIXME: there should be _some_ sensible error reporting
1435 * convention. See following comment too */
1436 fprintf(stderr,"Can't purify when more than one thread exists\n");
1442 printf("[doing purification:");
1445 #ifdef LISP_FEATURE_GENCGC
1446 gc_alloc_update_all_page_tables();
1448 for_each_thread(thread)
1449 if (fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX,thread)) != 0) {
1450 /* FIXME: 1. What does this mean? 2. It shouldn't be reporting
1451 * its error simply by a. printing a string b. to stdout instead
1453 printf(" Ack! Can't purify interrupt contexts. ");
1458 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
1459 dynamic_space_free_pointer =
1460 (lispobj*)SymbolValue(ALLOCATION_POINTER,0);
1463 read_only_end = read_only_free =
1464 (lispobj *)SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0);
1465 static_end = static_free =
1466 (lispobj *)SymbolValue(STATIC_SPACE_FREE_POINTER,0);
1473 #if defined(LISP_FEATURE_GENCGC) && (defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64))
1474 /* note this expects only one thread to be active. We'd have to
1475 * stop all the others in the same way as GC does if we wanted
1476 * PURIFY to work when >1 thread exists */
1477 setup_i386_stack_scav(((&static_roots)-2),
1478 ((void *)all_threads->control_stack_end));
1481 pscav(&static_roots, 1, 0);
1482 pscav(&read_only_roots, 1, 1);
1485 printf(" handlers");
1488 pscav((lispobj *) all_threads->interrupt_data->interrupt_handlers,
1489 sizeof(all_threads->interrupt_data->interrupt_handlers)
1497 #if !defined(LISP_FEATURE_X86) && !defined(LISP_FEATURE_X86_64)
1498 pscav((lispobj *)all_threads->control_stack_start,
1499 current_control_stack_pointer -
1500 all_threads->control_stack_start,
1503 #ifdef LISP_FEATURE_GENCGC
1509 printf(" bindings");
1512 #if !(defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64))
1513 pscav( (lispobj *)all_threads->binding_stack_start,
1514 (lispobj *)current_binding_stack_pointer -
1515 all_threads->binding_stack_start,
1518 for_each_thread(thread) {
1519 pscav( (lispobj *)thread->binding_stack_start,
1520 (lispobj *)SymbolValue(BINDING_STACK_POINTER,thread) -
1521 (lispobj *)thread->binding_stack_start,
1523 pscav( (lispobj *) (thread+1),
1524 fixnum_value(SymbolValue(FREE_TLS_INDEX,0)) -
1525 (sizeof (struct thread))/(sizeof (lispobj)),
1532 /* The original CMU CL code had scavenge-read-only-space code
1533 * controlled by the Lisp-level variable
1534 * *SCAVENGE-READ-ONLY-SPACE*. It was disabled by default, and it
1535 * wasn't documented under what circumstances it was useful or
1536 * safe to turn it on, so it's been turned off in SBCL. If you
1537 * want/need this functionality, and can test and document it,
1538 * please submit a patch. */
1540 if (SymbolValue(SCAVENGE_READ_ONLY_SPACE) != UNBOUND_MARKER_WIDETAG
1541 && SymbolValue(SCAVENGE_READ_ONLY_SPACE) != NIL) {
1542 unsigned read_only_space_size =
1543 (lispobj *)SymbolValue(READ_ONLY_SPACE_FREE_POINTER) -
1544 (lispobj *)READ_ONLY_SPACE_START;
1546 "scavenging read only space: %d bytes\n",
1547 read_only_space_size * sizeof(lispobj));
1548 pscav( (lispobj *)READ_ONLY_SPACE_START, read_only_space_size, 0);
1556 clean = (lispobj *)STATIC_SPACE_START;
1558 while (clean != static_free)
1559 clean = pscav(clean, static_free - clean, 0);
1560 laters = later_blocks;
1561 count = later_count;
1562 later_blocks = NULL;
1564 while (laters != NULL) {
1565 for (i = 0; i < count; i++) {
1566 if (laters->u[i].count == 0) {
1568 } else if (laters->u[i].count <= LATERMAXCOUNT) {
1569 pscav(laters->u[i+1].ptr, laters->u[i].count, 1);
1572 pscav(laters->u[i].ptr, 1, 1);
1575 next = laters->next;
1578 count = LATERBLOCKSIZE;
1580 } while (clean != static_free || later_blocks != NULL);
1587 os_zero((os_vm_address_t) current_dynamic_space,
1588 (os_vm_size_t) DYNAMIC_SPACE_SIZE);
1590 /* Zero the stack. Note that the stack is also zeroed by SUB-GC
1591 * calling SCRUB-CONTROL-STACK - this zeros the stack on the x86. */
1592 #if !defined(LISP_FEATURE_X86) && !defined(LISP_FEATURE_X86_64)
1593 os_zero((os_vm_address_t) current_control_stack_pointer,
1595 ((all_threads->control_stack_end -
1596 current_control_stack_pointer) * sizeof(lispobj)));
1599 /* It helps to update the heap free pointers so that free_heap can
1600 * verify after it's done. */
1601 SetSymbolValue(READ_ONLY_SPACE_FREE_POINTER, (lispobj)read_only_free,0);
1602 SetSymbolValue(STATIC_SPACE_FREE_POINTER, (lispobj)static_free,0);
1604 #if !defined(ALLOCATION_POINTER)
1605 dynamic_space_free_pointer = current_dynamic_space;
1606 set_auto_gc_trigger(bytes_consed_between_gcs);
1608 #if defined LISP_FEATURE_GENCGC
1611 #error unsupported case /* in CMU CL, was "ibmrt using GC" */
1615 /* Blast away instruction cache */
1616 os_flush_icache((os_vm_address_t)READ_ONLY_SPACE_START, READ_ONLY_SPACE_SIZE);
1617 os_flush_icache((os_vm_address_t)STATIC_SPACE_START, STATIC_SPACE_SIZE);