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);
130 if(read_only_free + nwords >= (lispobj *)READ_ONLY_SPACE_END) {
131 lose("Ran out of read-only space while purifying!");
134 read_only_free+=nwords;
136 if(static_free + nwords >= (lispobj *)STATIC_SPACE_END) {
137 lose("Ran out of static space while purifying!");
147 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
149 #ifdef LISP_FEATURE_GENCGC
151 * enhanced x86/GENCGC stack scavenging by Douglas Crosher
153 * Scavenging the stack on the i386 is problematic due to conservative
154 * roots and raw return addresses. Here it is handled in two passes:
155 * the first pass runs before any objects are moved and tries to
156 * identify valid pointers and return address on the stack, the second
157 * pass scavenges these.
160 static unsigned pointer_filter_verbose = 0;
162 /* FIXME: This is substantially the same code as
163 * possibly_valid_dynamic_space_pointer in gencgc.c. The only
164 * relevant difference seems to be that the gencgc code also checks
165 * for raw pointers into Code objects, whereas in purify these are
166 * checked separately in setup_i386_stack_scav - they go onto
167 * valid_stack_ra_locations instead of just valid_stack_locations */
170 valid_dynamic_space_pointer(lispobj *pointer, lispobj *start_addr)
172 /* If it's not a return address then it needs to be a valid Lisp
174 if (!is_lisp_pointer((lispobj)pointer))
177 /* Check that the object pointed to is consistent with the pointer
179 switch (lowtag_of((lispobj)pointer)) {
180 case FUN_POINTER_LOWTAG:
181 /* Start_addr should be the enclosing code object, or a closure
183 switch (widetag_of(*start_addr)) {
184 case CODE_HEADER_WIDETAG:
185 /* This case is probably caught above. */
187 case CLOSURE_HEADER_WIDETAG:
188 case FUNCALLABLE_INSTANCE_HEADER_WIDETAG:
189 if ((long)pointer != ((long)start_addr+FUN_POINTER_LOWTAG)) {
190 if (pointer_filter_verbose) {
191 fprintf(stderr,"*Wf2: %p %p %p\n",
192 pointer, start_addr, (void *)*start_addr);
198 if (pointer_filter_verbose) {
199 fprintf(stderr,"*Wf3: %p %p %p\n",
200 pointer, start_addr, (void *)*start_addr);
205 case LIST_POINTER_LOWTAG:
206 if ((long)pointer != ((long)start_addr+LIST_POINTER_LOWTAG)) {
207 if (pointer_filter_verbose)
208 fprintf(stderr,"*Wl1: %p %p %p\n",
209 pointer, start_addr, (void *)*start_addr);
212 /* Is it plausible cons? */
213 if ((is_lisp_pointer(start_addr[0])
214 || ((start_addr[0] & FIXNUM_TAG_MASK) == 0) /* fixnum */
215 || (widetag_of(start_addr[0]) == CHARACTER_WIDETAG)
216 #if N_WORD_BITS == 64
217 || (widetag_of(start_addr[0]) == SINGLE_FLOAT_WIDETAG)
219 || (widetag_of(start_addr[0]) == UNBOUND_MARKER_WIDETAG))
220 && (is_lisp_pointer(start_addr[1])
221 || ((start_addr[1] & FIXNUM_TAG_MASK) == 0) /* fixnum */
222 || (widetag_of(start_addr[1]) == CHARACTER_WIDETAG)
223 #if N_WORD_BITS == 64
224 || (widetag_of(start_addr[1]) == SINGLE_FLOAT_WIDETAG)
226 || (widetag_of(start_addr[1]) == UNBOUND_MARKER_WIDETAG))) {
229 if (pointer_filter_verbose) {
230 fprintf(stderr,"*Wl2: %p %p %p\n",
231 pointer, start_addr, (void *)*start_addr);
235 case INSTANCE_POINTER_LOWTAG:
236 if ((long)pointer != ((long)start_addr+INSTANCE_POINTER_LOWTAG)) {
237 if (pointer_filter_verbose) {
238 fprintf(stderr,"*Wi1: %p %p %p\n",
239 pointer, start_addr, (void *)*start_addr);
243 if (widetag_of(start_addr[0]) != INSTANCE_HEADER_WIDETAG) {
244 if (pointer_filter_verbose) {
245 fprintf(stderr,"*Wi2: %p %p %p\n",
246 pointer, start_addr, (void *)*start_addr);
251 case OTHER_POINTER_LOWTAG:
252 if ((long)pointer != ((long)start_addr+OTHER_POINTER_LOWTAG)) {
253 if (pointer_filter_verbose) {
254 fprintf(stderr,"*Wo1: %p %p %p\n",
255 pointer, start_addr, (void *)*start_addr);
259 /* Is it plausible? Not a cons. XXX should check the headers. */
260 if (is_lisp_pointer(start_addr[0]) || ((start_addr[0] & FIXNUM_TAG_MASK) == 0)) {
261 if (pointer_filter_verbose) {
262 fprintf(stderr,"*Wo2: %p %p %p\n",
263 pointer, start_addr, (void *)*start_addr);
267 switch (widetag_of(start_addr[0])) {
268 case UNBOUND_MARKER_WIDETAG:
269 case CHARACTER_WIDETAG:
270 #if N_WORD_BITS == 64
271 case SINGLE_FLOAT_WIDETAG:
273 if (pointer_filter_verbose) {
274 fprintf(stderr,"*Wo3: %p %p %p\n",
275 pointer, start_addr, (void *)*start_addr);
279 /* only pointed to by function pointers? */
280 case CLOSURE_HEADER_WIDETAG:
281 case FUNCALLABLE_INSTANCE_HEADER_WIDETAG:
282 if (pointer_filter_verbose) {
283 fprintf(stderr,"*Wo4: %p %p %p\n",
284 pointer, start_addr, (void *)*start_addr);
288 case INSTANCE_HEADER_WIDETAG:
289 if (pointer_filter_verbose) {
290 fprintf(stderr,"*Wo5: %p %p %p\n",
291 pointer, start_addr, (void *)*start_addr);
295 /* the valid other immediate pointer objects */
296 case SIMPLE_VECTOR_WIDETAG:
298 case COMPLEX_WIDETAG:
299 #ifdef COMPLEX_SINGLE_FLOAT_WIDETAG
300 case COMPLEX_SINGLE_FLOAT_WIDETAG:
302 #ifdef COMPLEX_DOUBLE_FLOAT_WIDETAG
303 case COMPLEX_DOUBLE_FLOAT_WIDETAG:
305 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
306 case COMPLEX_LONG_FLOAT_WIDETAG:
308 case SIMPLE_ARRAY_WIDETAG:
309 case COMPLEX_BASE_STRING_WIDETAG:
310 #ifdef COMPLEX_CHARACTER_STRING_WIDETAG
311 case COMPLEX_CHARACTER_STRING_WIDETAG:
313 case COMPLEX_VECTOR_NIL_WIDETAG:
314 case COMPLEX_BIT_VECTOR_WIDETAG:
315 case COMPLEX_VECTOR_WIDETAG:
316 case COMPLEX_ARRAY_WIDETAG:
317 case VALUE_CELL_HEADER_WIDETAG:
318 case SYMBOL_HEADER_WIDETAG:
320 case CODE_HEADER_WIDETAG:
322 #if N_WORD_BITS != 64
323 case SINGLE_FLOAT_WIDETAG:
325 case DOUBLE_FLOAT_WIDETAG:
326 #ifdef LONG_FLOAT_WIDETAG
327 case LONG_FLOAT_WIDETAG:
329 case SIMPLE_ARRAY_NIL_WIDETAG:
330 case SIMPLE_BASE_STRING_WIDETAG:
331 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
332 case SIMPLE_CHARACTER_STRING_WIDETAG:
334 case SIMPLE_BIT_VECTOR_WIDETAG:
335 case SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG:
336 case SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG:
337 case SIMPLE_ARRAY_UNSIGNED_BYTE_7_WIDETAG:
338 case SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG:
339 case SIMPLE_ARRAY_UNSIGNED_BYTE_15_WIDETAG:
340 case SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG:
341 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_29_WIDETAG
342 case SIMPLE_ARRAY_UNSIGNED_BYTE_29_WIDETAG:
344 case SIMPLE_ARRAY_UNSIGNED_BYTE_31_WIDETAG:
345 case SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG:
346 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_60_WIDETAG
347 case SIMPLE_ARRAY_UNSIGNED_BYTE_60_WIDETAG:
349 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG
350 case SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG:
352 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG
353 case SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG:
355 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
356 case SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG:
358 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
359 case SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG:
361 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG
362 case SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG:
364 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
365 case SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG:
367 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_61_WIDETAG
368 case SIMPLE_ARRAY_SIGNED_BYTE_61_WIDETAG:
370 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG
371 case SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG:
373 case SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG:
374 case SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG:
375 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
376 case SIMPLE_ARRAY_LONG_FLOAT_WIDETAG:
378 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
379 case SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG:
381 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
382 case SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG:
384 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
385 case SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG:
388 case WEAK_POINTER_WIDETAG:
392 if (pointer_filter_verbose) {
393 fprintf(stderr,"*Wo6: %p %p %p\n",
394 pointer, start_addr, (void *)*start_addr);
400 if (pointer_filter_verbose) {
401 fprintf(stderr,"*W?: %p %p %p\n",
402 pointer, start_addr, (void *)*start_addr);
411 #define MAX_STACK_POINTERS 256
412 lispobj *valid_stack_locations[MAX_STACK_POINTERS];
413 unsigned long num_valid_stack_locations;
415 #define MAX_STACK_RETURN_ADDRESSES 128
416 lispobj *valid_stack_ra_locations[MAX_STACK_RETURN_ADDRESSES];
417 lispobj *valid_stack_ra_code_objects[MAX_STACK_RETURN_ADDRESSES];
418 unsigned long num_valid_stack_ra_locations;
420 /* Identify valid stack slots. */
422 setup_i386_stack_scav(lispobj *lowaddr, lispobj *base)
424 lispobj *sp = lowaddr;
425 num_valid_stack_locations = 0;
426 num_valid_stack_ra_locations = 0;
427 for (sp = lowaddr; sp < base; sp++) {
429 /* Find the object start address */
430 lispobj *start_addr = search_dynamic_space((void *)thing);
432 /* We need to allow raw pointers into Code objects for
433 * return addresses. This will also pick up pointers to
434 * functions in code objects. */
435 if (widetag_of(*start_addr) == CODE_HEADER_WIDETAG) {
436 /* FIXME asserting here is a really dumb thing to do.
437 * If we've overflowed some arbitrary static limit, we
438 * should just refuse to purify, instead of killing
439 * the whole lisp session
441 gc_assert(num_valid_stack_ra_locations <
442 MAX_STACK_RETURN_ADDRESSES);
443 valid_stack_ra_locations[num_valid_stack_ra_locations] = sp;
444 valid_stack_ra_code_objects[num_valid_stack_ra_locations++] =
445 (lispobj *)((long)start_addr + OTHER_POINTER_LOWTAG);
447 if (valid_dynamic_space_pointer((void *)thing, start_addr)) {
448 gc_assert(num_valid_stack_locations < MAX_STACK_POINTERS);
449 valid_stack_locations[num_valid_stack_locations++] = sp;
454 if (pointer_filter_verbose) {
455 fprintf(stderr, "number of valid stack pointers = %ld\n",
456 num_valid_stack_locations);
457 fprintf(stderr, "number of stack return addresses = %ld\n",
458 num_valid_stack_ra_locations);
463 pscav_i386_stack(void)
467 for (i = 0; i < num_valid_stack_locations; i++)
468 pscav(valid_stack_locations[i], 1, 0);
470 for (i = 0; i < num_valid_stack_ra_locations; i++) {
471 lispobj code_obj = (lispobj)valid_stack_ra_code_objects[i];
472 pscav(&code_obj, 1, 0);
473 if (pointer_filter_verbose) {
474 fprintf(stderr,"*C moved RA %p to %p; for code object %p to %p\n",
475 (void *)*valid_stack_ra_locations[i],
476 (void *)(*valid_stack_ra_locations[i]) -
477 ((void *)valid_stack_ra_code_objects[i] -
479 valid_stack_ra_code_objects[i], (void *)code_obj);
481 *valid_stack_ra_locations[i] =
482 ((long)(*valid_stack_ra_locations[i])
483 - ((long)valid_stack_ra_code_objects[i] - (long)code_obj));
491 pscav_later(lispobj *where, long count)
495 if (count > LATERMAXCOUNT) {
496 while (count > LATERMAXCOUNT) {
497 pscav_later(where, LATERMAXCOUNT);
498 count -= LATERMAXCOUNT;
499 where += LATERMAXCOUNT;
503 if (later_blocks == NULL || later_count == LATERBLOCKSIZE ||
504 (later_count == LATERBLOCKSIZE-1 && count > 1)) {
505 new = (struct later *)malloc(sizeof(struct later));
506 new->next = later_blocks;
507 if (later_blocks && later_count < LATERBLOCKSIZE)
508 later_blocks->u[later_count].ptr = NULL;
514 later_blocks->u[later_count++].count = count;
515 later_blocks->u[later_count++].ptr = where;
520 ptrans_boxed(lispobj thing, lispobj header, boolean constant)
523 lispobj result, *new, *old;
525 nwords = CEILING(1 + HeaderValue(header), 2);
528 old = (lispobj *)native_pointer(thing);
529 new = newspace_alloc(nwords,constant);
532 bcopy(old, new, nwords * sizeof(lispobj));
534 /* Deposit forwarding pointer. */
535 result = make_lispobj(new, lowtag_of(thing));
539 pscav(new, nwords, constant);
544 /* We need to look at the layout to see whether it is a pure structure
545 * class, and only then can we transport as constant. If it is pure,
546 * we can ALWAYS transport as a constant. */
548 ptrans_instance(lispobj thing, lispobj header, boolean /* ignored */ constant)
550 lispobj layout = ((struct instance *)native_pointer(thing))->slots[0];
551 lispobj pure = ((struct instance *)native_pointer(layout))->slots[15];
555 return (ptrans_boxed(thing, header, 1));
557 return (ptrans_boxed(thing, header, 0));
560 /* Substructure: special case for the COMPACT-INFO-ENVs,
561 * where the instance may have a point to the dynamic
562 * space placed into it (e.g. the cache-name slot), but
563 * the lists and arrays at the time of a purify can be
564 * moved to the RO space. */
566 lispobj result, *new, *old;
568 nwords = CEILING(1 + HeaderValue(header), 2);
571 old = (lispobj *)native_pointer(thing);
572 new = newspace_alloc(nwords, 0); /* inconstant */
575 bcopy(old, new, nwords * sizeof(lispobj));
577 /* Deposit forwarding pointer. */
578 result = make_lispobj(new, lowtag_of(thing));
582 pscav(new, nwords, 1);
588 return NIL; /* dummy value: return something ... */
593 ptrans_fdefn(lispobj thing, lispobj header)
596 lispobj result, *new, *old, oldfn;
599 nwords = CEILING(1 + HeaderValue(header), 2);
602 old = (lispobj *)native_pointer(thing);
603 new = newspace_alloc(nwords, 0); /* inconstant */
606 bcopy(old, new, nwords * sizeof(lispobj));
608 /* Deposit forwarding pointer. */
609 result = make_lispobj(new, lowtag_of(thing));
612 /* Scavenge the function. */
613 fdefn = (struct fdefn *)new;
615 pscav(&fdefn->fun, 1, 0);
616 if ((char *)oldfn + FUN_RAW_ADDR_OFFSET == fdefn->raw_addr)
617 fdefn->raw_addr = (char *)fdefn->fun + FUN_RAW_ADDR_OFFSET;
623 ptrans_unboxed(lispobj thing, lispobj header)
626 lispobj result, *new, *old;
628 nwords = CEILING(1 + HeaderValue(header), 2);
631 old = (lispobj *)native_pointer(thing);
632 new = newspace_alloc(nwords,1); /* always constant */
635 bcopy(old, new, nwords * sizeof(lispobj));
637 /* Deposit forwarding pointer. */
638 result = make_lispobj(new , lowtag_of(thing));
645 ptrans_vector(lispobj thing, long bits, long extra,
646 boolean boxed, boolean constant)
648 struct vector *vector;
650 lispobj result, *new;
653 vector = (struct vector *)native_pointer(thing);
654 length = fixnum_value(vector->length)+extra;
655 // Argh, handle simple-vector-nil separately.
659 nwords = CEILING(NWORDS(length, bits) + 2, 2);
662 new=newspace_alloc(nwords, (constant || !boxed));
663 bcopy(vector, new, nwords * sizeof(lispobj));
665 result = make_lispobj(new, lowtag_of(thing));
666 vector->header = result;
669 pscav(new, nwords, constant);
674 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
676 apply_code_fixups_during_purify(struct code *old_code, struct code *new_code)
678 long nheader_words, ncode_words, nwords;
679 void *constants_start_addr, *constants_end_addr;
680 void *code_start_addr, *code_end_addr;
681 lispobj fixups = NIL;
682 unsigned displacement = (unsigned)new_code - (unsigned)old_code;
683 struct vector *fixups_vector;
685 ncode_words = fixnum_value(new_code->code_size);
686 nheader_words = HeaderValue(*(lispobj *)new_code);
687 nwords = ncode_words + nheader_words;
689 constants_start_addr = (void *)new_code + 5 * N_WORD_BYTES;
690 constants_end_addr = (void *)new_code + nheader_words*N_WORD_BYTES;
691 code_start_addr = (void *)new_code + nheader_words*N_WORD_BYTES;
692 code_end_addr = (void *)new_code + nwords*N_WORD_BYTES;
694 /* The first constant should be a pointer to the fixups for this
695 * code objects. Check. */
696 fixups = new_code->constants[0];
698 /* It will be 0 or the unbound-marker if there are no fixups, and
699 * will be an other-pointer to a vector if it is valid. */
701 (fixups==UNBOUND_MARKER_WIDETAG) ||
702 !is_lisp_pointer(fixups)) {
703 #ifdef LISP_FEATURE_GENCGC
704 /* Check for a possible errors. */
705 sniff_code_object(new_code,displacement);
710 fixups_vector = (struct vector *)native_pointer(fixups);
712 /* Could be pointing to a forwarding pointer. */
713 if (is_lisp_pointer(fixups) && (dynamic_pointer_p(fixups))
714 && forwarding_pointer_p(*(lispobj *)fixups_vector)) {
715 /* If so then follow it. */
717 (struct vector *)native_pointer(*(lispobj *)fixups_vector);
720 if (widetag_of(fixups_vector->header) == SIMPLE_ARRAY_WORD_WIDETAG) {
721 /* We got the fixups for the code block. Now work through the
722 * vector, and apply a fixup at each address. */
723 long length = fixnum_value(fixups_vector->length);
725 for (i=0; i<length; i++) {
726 unsigned offset = fixups_vector->data[i];
727 /* Now check the current value of offset. */
729 *(unsigned *)((unsigned)code_start_addr + offset);
731 /* If it's within the old_code object then it must be an
732 * absolute fixup (relative ones are not saved) */
733 if ((old_value>=(unsigned)old_code)
734 && (old_value<((unsigned)old_code + nwords * N_WORD_BYTES)))
735 /* So add the dispacement. */
736 *(unsigned *)((unsigned)code_start_addr + offset) = old_value
739 /* It is outside the old code object so it must be a relative
740 * fixup (absolute fixups are not saved). So subtract the
742 *(unsigned *)((unsigned)code_start_addr + offset) = old_value
747 /* No longer need the fixups. */
748 new_code->constants[0] = 0;
750 #ifdef LISP_FEATURE_GENCGC
751 /* Check for possible errors. */
752 sniff_code_object(new_code,displacement);
758 ptrans_code(lispobj thing)
760 struct code *code, *new;
762 lispobj func, result;
764 code = (struct code *)native_pointer(thing);
765 nwords = CEILING(HeaderValue(code->header) + fixnum_value(code->code_size),
768 new = (struct code *)newspace_alloc(nwords,1); /* constant */
770 bcopy(code, new, nwords * sizeof(lispobj));
772 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
773 apply_code_fixups_during_purify(code,new);
776 result = make_lispobj(new, OTHER_POINTER_LOWTAG);
778 /* Stick in a forwarding pointer for the code object. */
779 *(lispobj *)code = result;
781 /* Put in forwarding pointers for all the functions. */
782 for (func = code->entry_points;
784 func = ((struct simple_fun *)native_pointer(func))->next) {
786 gc_assert(lowtag_of(func) == FUN_POINTER_LOWTAG);
788 *(lispobj *)native_pointer(func) = result + (func - thing);
791 /* Arrange to scavenge the debug info later. */
792 pscav_later(&new->debug_info, 1);
794 /* FIXME: why would this be a fixnum? */
795 /* "why" is a hard word, but apparently for compiled functions the
796 trace_table_offset contains the length of the instructions, as
797 a fixnum. See CODE-INST-AREA-LENGTH in
798 src/compiler/target-disassem.lisp. -- CSR, 2004-01-08 */
799 if (!(fixnump(new->trace_table_offset)))
801 pscav(&new->trace_table_offset, 1, 0);
803 new->trace_table_offset = NIL; /* limit lifetime */
806 /* Scavenge the constants. */
807 pscav(new->constants, HeaderValue(new->header)-5, 1);
809 /* Scavenge all the functions. */
810 pscav(&new->entry_points, 1, 1);
811 for (func = new->entry_points;
813 func = ((struct simple_fun *)native_pointer(func))->next) {
814 gc_assert(lowtag_of(func) == FUN_POINTER_LOWTAG);
815 gc_assert(!dynamic_pointer_p(func));
817 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
818 /* Temporarily convert the self pointer to a real function pointer. */
819 ((struct simple_fun *)native_pointer(func))->self
820 -= FUN_RAW_ADDR_OFFSET;
822 pscav(&((struct simple_fun *)native_pointer(func))->self, 2, 1);
823 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
824 ((struct simple_fun *)native_pointer(func))->self
825 += FUN_RAW_ADDR_OFFSET;
827 pscav_later(&((struct simple_fun *)native_pointer(func))->name, 3);
834 ptrans_func(lispobj thing, lispobj header)
837 lispobj code, *new, *old, result;
838 struct simple_fun *function;
840 /* Thing can either be a function header, a closure function
841 * header, a closure, or a funcallable-instance. If it's a closure
842 * or a funcallable-instance, we do the same as ptrans_boxed.
843 * Otherwise we have to do something strange, 'cause it is buried
844 * inside a code object. */
846 if (widetag_of(header) == SIMPLE_FUN_HEADER_WIDETAG) {
848 /* We can only end up here if the code object has not been
849 * scavenged, because if it had been scavenged, forwarding pointers
850 * would have been left behind for all the entry points. */
852 function = (struct simple_fun *)native_pointer(thing);
855 ((native_pointer(thing) -
856 (HeaderValue(function->header))), OTHER_POINTER_LOWTAG);
858 /* This will cause the function's header to be replaced with a
859 * forwarding pointer. */
863 /* So we can just return that. */
864 return function->header;
867 /* It's some kind of closure-like thing. */
868 nwords = CEILING(1 + HeaderValue(header), 2);
869 old = (lispobj *)native_pointer(thing);
871 /* Allocate the new one. FINs *must* not go in read_only
872 * space. Closures can; they never change */
875 (nwords,(widetag_of(header)!=FUNCALLABLE_INSTANCE_HEADER_WIDETAG));
878 bcopy(old, new, nwords * sizeof(lispobj));
880 /* Deposit forwarding pointer. */
881 result = make_lispobj(new, lowtag_of(thing));
885 pscav(new, nwords, 0);
892 ptrans_returnpc(lispobj thing, lispobj header)
896 /* Find the corresponding code object. */
897 code = thing - HeaderValue(header)*sizeof(lispobj);
899 /* Make sure it's been transported. */
900 new = *(lispobj *)native_pointer(code);
901 if (!forwarding_pointer_p(new))
902 new = ptrans_code(code);
904 /* Maintain the offset: */
905 return new + (thing - code);
908 #define WORDS_PER_CONS CEILING(sizeof(struct cons) / sizeof(lispobj), 2)
911 ptrans_list(lispobj thing, boolean constant)
913 struct cons *old, *new, *orig;
916 orig = (struct cons *) newspace_alloc(0,constant);
920 /* Allocate a new cons cell. */
921 old = (struct cons *)native_pointer(thing);
922 new = (struct cons *) newspace_alloc(WORDS_PER_CONS,constant);
924 /* Copy the cons cell and keep a pointer to the cdr. */
926 thing = new->cdr = old->cdr;
928 /* Set up the forwarding pointer. */
929 *(lispobj *)old = make_lispobj(new, LIST_POINTER_LOWTAG);
931 /* And count this cell. */
933 } while (lowtag_of(thing) == LIST_POINTER_LOWTAG &&
934 dynamic_pointer_p(thing) &&
935 !(forwarding_pointer_p(*(lispobj *)native_pointer(thing))));
937 /* Scavenge the list we just copied. */
938 pscav((lispobj *)orig, length * WORDS_PER_CONS, constant);
940 return make_lispobj(orig, LIST_POINTER_LOWTAG);
944 ptrans_otherptr(lispobj thing, lispobj header, boolean constant)
946 switch (widetag_of(header)) {
947 /* FIXME: this needs a reindent */
949 case SINGLE_FLOAT_WIDETAG:
950 case DOUBLE_FLOAT_WIDETAG:
951 #ifdef LONG_FLOAT_WIDETAG
952 case LONG_FLOAT_WIDETAG:
954 #ifdef COMPLEX_SINGLE_FLOAT_WIDETAG
955 case COMPLEX_SINGLE_FLOAT_WIDETAG:
957 #ifdef COMPLEX_DOUBLE_FLOAT_WIDETAG
958 case COMPLEX_DOUBLE_FLOAT_WIDETAG:
960 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
961 case COMPLEX_LONG_FLOAT_WIDETAG:
964 return ptrans_unboxed(thing, header);
967 case COMPLEX_WIDETAG:
968 case SIMPLE_ARRAY_WIDETAG:
969 case COMPLEX_BASE_STRING_WIDETAG:
970 #ifdef COMPLEX_CHARACTER_STRING_WIDETAG
971 case COMPLEX_CHARACTER_STRING_WIDETAG:
973 case COMPLEX_BIT_VECTOR_WIDETAG:
974 case COMPLEX_VECTOR_NIL_WIDETAG:
975 case COMPLEX_VECTOR_WIDETAG:
976 case COMPLEX_ARRAY_WIDETAG:
977 return ptrans_boxed(thing, header, constant);
979 case VALUE_CELL_HEADER_WIDETAG:
980 case WEAK_POINTER_WIDETAG:
981 return ptrans_boxed(thing, header, 0);
983 case SYMBOL_HEADER_WIDETAG:
984 return ptrans_boxed(thing, header, 0);
986 case SIMPLE_ARRAY_NIL_WIDETAG:
987 return ptrans_vector(thing, 0, 0, 0, constant);
989 case SIMPLE_BASE_STRING_WIDETAG:
990 return ptrans_vector(thing, 8, 1, 0, constant);
992 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
993 case SIMPLE_CHARACTER_STRING_WIDETAG:
994 return ptrans_vector(thing, 32, 1, 0, constant);
997 case SIMPLE_BIT_VECTOR_WIDETAG:
998 return ptrans_vector(thing, 1, 0, 0, constant);
1000 case SIMPLE_VECTOR_WIDETAG:
1001 return ptrans_vector(thing, N_WORD_BITS, 0, 1, constant);
1003 case SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG:
1004 return ptrans_vector(thing, 2, 0, 0, constant);
1006 case SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG:
1007 return ptrans_vector(thing, 4, 0, 0, constant);
1009 case SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG:
1010 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
1011 case SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG:
1012 case SIMPLE_ARRAY_UNSIGNED_BYTE_7_WIDETAG:
1014 return ptrans_vector(thing, 8, 0, 0, constant);
1016 case SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG:
1017 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
1018 case SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG:
1019 case SIMPLE_ARRAY_UNSIGNED_BYTE_15_WIDETAG:
1021 return ptrans_vector(thing, 16, 0, 0, constant);
1023 case SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG:
1024 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG
1025 case SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG:
1026 case SIMPLE_ARRAY_UNSIGNED_BYTE_29_WIDETAG:
1028 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
1029 case SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG:
1030 case SIMPLE_ARRAY_UNSIGNED_BYTE_31_WIDETAG:
1032 return ptrans_vector(thing, 32, 0, 0, constant);
1034 #if N_WORD_BITS == 64
1035 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_60_WIDETAG
1036 case SIMPLE_ARRAY_UNSIGNED_BYTE_60_WIDETAG:
1038 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG
1039 case SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG:
1041 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG
1042 case SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG:
1044 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_61_WIDETAG
1045 case SIMPLE_ARRAY_SIGNED_BYTE_61_WIDETAG:
1047 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG
1048 case SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG:
1050 return ptrans_vector(thing, 64, 0, 0, constant);
1053 case SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG:
1054 return ptrans_vector(thing, 32, 0, 0, constant);
1056 case SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG:
1057 return ptrans_vector(thing, 64, 0, 0, constant);
1059 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
1060 case SIMPLE_ARRAY_LONG_FLOAT_WIDETAG:
1061 #ifdef LISP_FEATURE_X86
1062 return ptrans_vector(thing, 96, 0, 0, constant);
1064 #ifdef LISP_FEATURE_SPARC
1065 return ptrans_vector(thing, 128, 0, 0, constant);
1069 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
1070 case SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG:
1071 return ptrans_vector(thing, 64, 0, 0, constant);
1074 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
1075 case SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG:
1076 return ptrans_vector(thing, 128, 0, 0, constant);
1079 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
1080 case SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG:
1081 #ifdef LISP_FEATURE_X86
1082 return ptrans_vector(thing, 192, 0, 0, constant);
1084 #ifdef LISP_FEATURE_SPARC
1085 return ptrans_vector(thing, 256, 0, 0, constant);
1089 case CODE_HEADER_WIDETAG:
1090 return ptrans_code(thing);
1092 case RETURN_PC_HEADER_WIDETAG:
1093 return ptrans_returnpc(thing, header);
1096 return ptrans_fdefn(thing, header);
1099 fprintf(stderr, "Invalid widetag: %d\n", widetag_of(header));
1100 /* Should only come across other pointers to the above stuff. */
1107 pscav_fdefn(struct fdefn *fdefn)
1111 fix_func = ((char *)(fdefn->fun+FUN_RAW_ADDR_OFFSET) == fdefn->raw_addr);
1112 pscav(&fdefn->name, 1, 1);
1113 pscav(&fdefn->fun, 1, 0);
1115 fdefn->raw_addr = (char *)(fdefn->fun + FUN_RAW_ADDR_OFFSET);
1116 return sizeof(struct fdefn) / sizeof(lispobj);
1119 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
1120 /* now putting code objects in static space */
1122 pscav_code(struct code*code)
1126 nwords = CEILING(HeaderValue(code->header) + fixnum_value(code->code_size),
1129 /* Arrange to scavenge the debug info later. */
1130 pscav_later(&code->debug_info, 1);
1132 /* Scavenge the constants. */
1133 pscav(code->constants, HeaderValue(code->header)-5, 1);
1135 /* Scavenge all the functions. */
1136 pscav(&code->entry_points, 1, 1);
1137 for (func = code->entry_points;
1139 func = ((struct simple_fun *)native_pointer(func))->next) {
1140 gc_assert(lowtag_of(func) == FUN_POINTER_LOWTAG);
1141 gc_assert(!dynamic_pointer_p(func));
1143 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
1144 /* Temporarily convert the self pointer to a real function
1146 ((struct simple_fun *)native_pointer(func))->self
1147 -= FUN_RAW_ADDR_OFFSET;
1149 pscav(&((struct simple_fun *)native_pointer(func))->self, 2, 1);
1150 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
1151 ((struct simple_fun *)native_pointer(func))->self
1152 += FUN_RAW_ADDR_OFFSET;
1154 pscav_later(&((struct simple_fun *)native_pointer(func))->name, 3);
1157 return CEILING(nwords,2);
1162 pscav(lispobj *addr, long nwords, boolean constant)
1164 lispobj thing, *thingp, header;
1165 long count = 0; /* (0 = dummy init value to stop GCC warning) */
1166 struct vector *vector;
1168 while (nwords > 0) {
1170 if (is_lisp_pointer(thing)) {
1171 /* It's a pointer. Is it something we might have to move? */
1172 if (dynamic_pointer_p(thing)) {
1173 /* Maybe. Have we already moved it? */
1174 thingp = (lispobj *)native_pointer(thing);
1176 if (is_lisp_pointer(header) && forwarding_pointer_p(header))
1177 /* Yep, so just copy the forwarding pointer. */
1180 /* Nope, copy the object. */
1181 switch (lowtag_of(thing)) {
1182 case FUN_POINTER_LOWTAG:
1183 thing = ptrans_func(thing, header);
1186 case LIST_POINTER_LOWTAG:
1187 thing = ptrans_list(thing, constant);
1190 case INSTANCE_POINTER_LOWTAG:
1191 thing = ptrans_instance(thing, header, constant);
1194 case OTHER_POINTER_LOWTAG:
1195 thing = ptrans_otherptr(thing, header, constant);
1199 /* It was a pointer, but not one of them? */
1207 #if N_WORD_BITS == 64
1208 else if (widetag_of(thing) == SINGLE_FLOAT_WIDETAG) {
1212 else if (thing & FIXNUM_TAG_MASK) {
1213 /* It's an other immediate. Maybe the header for an unboxed */
1215 switch (widetag_of(thing)) {
1216 case BIGNUM_WIDETAG:
1217 case SINGLE_FLOAT_WIDETAG:
1218 case DOUBLE_FLOAT_WIDETAG:
1219 #ifdef LONG_FLOAT_WIDETAG
1220 case LONG_FLOAT_WIDETAG:
1223 /* It's an unboxed simple object. */
1224 count = CEILING(HeaderValue(thing)+1, 2);
1227 case SIMPLE_VECTOR_WIDETAG:
1228 if (HeaderValue(thing) == subtype_VectorValidHashing) {
1229 *addr = (subtype_VectorMustRehash << N_WIDETAG_BITS) |
1230 SIMPLE_VECTOR_WIDETAG;
1235 case SIMPLE_ARRAY_NIL_WIDETAG:
1239 case SIMPLE_BASE_STRING_WIDETAG:
1240 vector = (struct vector *)addr;
1241 count = CEILING(NWORDS(fixnum_value(vector->length)+1,8)+2,2);
1244 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
1245 case SIMPLE_CHARACTER_STRING_WIDETAG:
1246 vector = (struct vector *)addr;
1247 count = CEILING(NWORDS(fixnum_value(vector->length)+1,32)+2,2);
1251 case SIMPLE_BIT_VECTOR_WIDETAG:
1252 vector = (struct vector *)addr;
1253 count = CEILING(NWORDS(fixnum_value(vector->length),1)+2,2);
1256 case SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG:
1257 vector = (struct vector *)addr;
1258 count = CEILING(NWORDS(fixnum_value(vector->length),2)+2,2);
1261 case SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG:
1262 vector = (struct vector *)addr;
1263 count = CEILING(NWORDS(fixnum_value(vector->length),4)+2,2);
1266 case SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG:
1267 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
1268 case SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG:
1269 case SIMPLE_ARRAY_UNSIGNED_BYTE_7_WIDETAG:
1271 vector = (struct vector *)addr;
1272 count = CEILING(NWORDS(fixnum_value(vector->length),8)+2,2);
1275 case SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG:
1276 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
1277 case SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG:
1278 case SIMPLE_ARRAY_UNSIGNED_BYTE_15_WIDETAG:
1280 vector = (struct vector *)addr;
1281 count = CEILING(NWORDS(fixnum_value(vector->length),16)+2,2);
1284 case SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG:
1285 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG
1286 case SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG:
1287 case SIMPLE_ARRAY_UNSIGNED_BYTE_29_WIDETAG:
1289 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
1290 case SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG:
1291 case SIMPLE_ARRAY_UNSIGNED_BYTE_31_WIDETAG:
1293 vector = (struct vector *)addr;
1294 count = CEILING(NWORDS(fixnum_value(vector->length),32)+2,2);
1297 #if N_WORD_BITS == 64
1298 case SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG:
1299 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_61_WIDETAG
1300 case SIMPLE_ARRAY_SIGNED_BYTE_61_WIDETAG:
1301 case SIMPLE_ARRAY_UNSIGNED_BYTE_60_WIDETAG:
1303 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG
1304 case SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG:
1305 case SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG:
1307 vector = (struct vector *)addr;
1308 count = CEILING(NWORDS(fixnum_value(vector->length),64)+2,2);
1312 case SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG:
1313 vector = (struct vector *)addr;
1314 count = CEILING(NWORDS(fixnum_value(vector->length), 32) + 2,
1318 case SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG:
1319 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
1320 case SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG:
1322 vector = (struct vector *)addr;
1323 count = CEILING(NWORDS(fixnum_value(vector->length), 64) + 2,
1327 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
1328 case SIMPLE_ARRAY_LONG_FLOAT_WIDETAG:
1329 vector = (struct vector *)addr;
1330 #ifdef LISP_FEATURE_X86
1331 count = fixnum_value(vector->length)*3+2;
1333 #ifdef LISP_FEATURE_SPARC
1334 count = fixnum_value(vector->length)*4+2;
1339 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
1340 case SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG:
1341 vector = (struct vector *)addr;
1342 count = CEILING(NWORDS(fixnum_value(vector->length), 128) + 2,
1347 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
1348 case SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG:
1349 vector = (struct vector *)addr;
1350 #ifdef LISP_FEATURE_X86
1351 count = fixnum_value(vector->length)*6+2;
1353 #ifdef LISP_FEATURE_SPARC
1354 count = fixnum_value(vector->length)*8+2;
1359 case CODE_HEADER_WIDETAG:
1360 #if !defined(LISP_FEATURE_X86) && !defined(LISP_FEATURE_X86_64)
1361 gc_abort(); /* no code headers in static space */
1363 count = pscav_code((struct code*)addr);
1367 case SIMPLE_FUN_HEADER_WIDETAG:
1368 case RETURN_PC_HEADER_WIDETAG:
1369 /* We should never hit any of these, 'cause they occur
1370 * buried in the middle of code objects. */
1374 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
1375 case CLOSURE_HEADER_WIDETAG:
1376 case FUNCALLABLE_INSTANCE_HEADER_WIDETAG:
1377 /* The function self pointer needs special care on the
1378 * x86 because it is the real entry point. */
1380 lispobj fun = ((struct closure *)addr)->fun
1381 - FUN_RAW_ADDR_OFFSET;
1382 pscav(&fun, 1, constant);
1383 ((struct closure *)addr)->fun = fun + FUN_RAW_ADDR_OFFSET;
1389 case WEAK_POINTER_WIDETAG:
1390 /* Weak pointers get preserved during purify, 'cause I
1391 * don't feel like figuring out how to break them. */
1392 pscav(addr+1, 2, constant);
1397 /* We have to handle fdefn objects specially, so we
1398 * can fix up the raw function address. */
1399 count = pscav_fdefn((struct fdefn *)addr);
1402 case INSTANCE_HEADER_WIDETAG:
1404 struct instance *instance = (struct instance *) addr;
1405 struct layout *layout
1406 = (struct layout *) native_pointer(instance->slots[0]);
1407 long nuntagged = fixnum_value(layout->n_untagged_slots);
1408 long nslots = HeaderValue(*addr);
1409 pscav(addr + 1, nslots - nuntagged, constant);
1410 count = CEILING(1 + nslots, 2);
1420 /* It's a fixnum. */
1432 purify(lispobj static_roots, lispobj read_only_roots)
1436 struct later *laters, *next;
1437 struct thread *thread;
1439 if(all_threads->next) {
1440 /* FIXME: there should be _some_ sensible error reporting
1441 * convention. See following comment too */
1442 fprintf(stderr,"Can't purify when more than one thread exists\n");
1448 printf("[doing purification:");
1451 #ifdef LISP_FEATURE_GENCGC
1452 gc_alloc_update_all_page_tables();
1454 for_each_thread(thread)
1455 if (fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX,thread)) != 0) {
1456 /* FIXME: 1. What does this mean? 2. It shouldn't be reporting
1457 * its error simply by a. printing a string b. to stdout instead
1459 printf(" Ack! Can't purify interrupt contexts. ");
1464 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
1465 dynamic_space_free_pointer =
1466 (lispobj*)SymbolValue(ALLOCATION_POINTER,0);
1469 read_only_end = read_only_free =
1470 (lispobj *)SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0);
1471 static_end = static_free =
1472 (lispobj *)SymbolValue(STATIC_SPACE_FREE_POINTER,0);
1479 #if defined(LISP_FEATURE_GENCGC) && (defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64))
1480 /* note this expects only one thread to be active. We'd have to
1481 * stop all the others in the same way as GC does if we wanted
1482 * PURIFY to work when >1 thread exists */
1483 setup_i386_stack_scav(((&static_roots)-2),
1484 ((void *)all_threads->control_stack_end));
1487 pscav(&static_roots, 1, 0);
1488 pscav(&read_only_roots, 1, 1);
1491 printf(" handlers");
1494 pscav((lispobj *) interrupt_handlers,
1495 sizeof(interrupt_handlers) / sizeof(lispobj),
1502 #if !defined(LISP_FEATURE_X86) && !defined(LISP_FEATURE_X86_64)
1503 pscav((lispobj *)all_threads->control_stack_start,
1504 current_control_stack_pointer -
1505 all_threads->control_stack_start,
1508 #ifdef LISP_FEATURE_GENCGC
1514 printf(" bindings");
1517 #if !(defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64))
1518 pscav( (lispobj *)all_threads->binding_stack_start,
1519 (lispobj *)current_binding_stack_pointer -
1520 all_threads->binding_stack_start,
1523 for_each_thread(thread) {
1524 pscav( (lispobj *)thread->binding_stack_start,
1525 (lispobj *)SymbolValue(BINDING_STACK_POINTER,thread) -
1526 (lispobj *)thread->binding_stack_start,
1528 pscav( (lispobj *) (thread+1),
1529 fixnum_value(SymbolValue(FREE_TLS_INDEX,0)) -
1530 (sizeof (struct thread))/(sizeof (lispobj)),
1537 /* The original CMU CL code had scavenge-read-only-space code
1538 * controlled by the Lisp-level variable
1539 * *SCAVENGE-READ-ONLY-SPACE*. It was disabled by default, and it
1540 * wasn't documented under what circumstances it was useful or
1541 * safe to turn it on, so it's been turned off in SBCL. If you
1542 * want/need this functionality, and can test and document it,
1543 * please submit a patch. */
1545 if (SymbolValue(SCAVENGE_READ_ONLY_SPACE) != UNBOUND_MARKER_WIDETAG
1546 && SymbolValue(SCAVENGE_READ_ONLY_SPACE) != NIL) {
1547 unsigned read_only_space_size =
1548 (lispobj *)SymbolValue(READ_ONLY_SPACE_FREE_POINTER) -
1549 (lispobj *)READ_ONLY_SPACE_START;
1551 "scavenging read only space: %d bytes\n",
1552 read_only_space_size * sizeof(lispobj));
1553 pscav( (lispobj *)READ_ONLY_SPACE_START, read_only_space_size, 0);
1561 clean = (lispobj *)STATIC_SPACE_START;
1563 while (clean != static_free)
1564 clean = pscav(clean, static_free - clean, 0);
1565 laters = later_blocks;
1566 count = later_count;
1567 later_blocks = NULL;
1569 while (laters != NULL) {
1570 for (i = 0; i < count; i++) {
1571 if (laters->u[i].count == 0) {
1573 } else if (laters->u[i].count <= LATERMAXCOUNT) {
1574 pscav(laters->u[i+1].ptr, laters->u[i].count, 1);
1577 pscav(laters->u[i].ptr, 1, 1);
1580 next = laters->next;
1583 count = LATERBLOCKSIZE;
1585 } while (clean != static_free || later_blocks != NULL);
1592 os_zero((os_vm_address_t) current_dynamic_space,
1593 (os_vm_size_t) DYNAMIC_SPACE_SIZE);
1595 /* Zero the stack. Note that the stack is also zeroed by SUB-GC
1596 * calling SCRUB-CONTROL-STACK - this zeros the stack on the x86. */
1597 #if !defined(LISP_FEATURE_X86) && !defined(LISP_FEATURE_X86_64)
1598 os_zero((os_vm_address_t) current_control_stack_pointer,
1600 ((all_threads->control_stack_end -
1601 current_control_stack_pointer) * sizeof(lispobj)));
1604 /* It helps to update the heap free pointers so that free_heap can
1605 * verify after it's done. */
1606 SetSymbolValue(READ_ONLY_SPACE_FREE_POINTER, (lispobj)read_only_free,0);
1607 SetSymbolValue(STATIC_SPACE_FREE_POINTER, (lispobj)static_free,0);
1609 #if !defined(ALLOCATION_POINTER)
1610 dynamic_space_free_pointer = current_dynamic_space;
1611 set_auto_gc_trigger(bytes_consed_between_gcs);
1613 #if defined LISP_FEATURE_GENCGC
1616 #error unsupported case /* in CMU CL, was "ibmrt using GC" */
1620 /* Blast away instruction cache */
1621 os_flush_icache((os_vm_address_t)READ_ONLY_SPACE_START, READ_ONLY_SPACE_SIZE);
1622 os_flush_icache((os_vm_address_t)STATIC_SPACE_START, STATIC_SPACE_SIZE);