0.7.13.5
[sbcl/lichteblau.git] / src / runtime / purify.c
bloba67f338dd4f553b08cba97fa4c2935b793a4ad65
1 /*
2 * C-level stuff to implement Lisp-level PURIFY
3 */
5 /*
6 * This software is part of the SBCL system. See the README file for
7 * more information.
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.
16 #include <stdio.h>
17 #include <sys/types.h>
18 #include <stdlib.h>
19 #include <strings.h>
21 #include "runtime.h"
22 #include "os.h"
23 #include "sbcl.h"
24 #include "globals.h"
25 #include "validate.h"
26 #include "interrupt.h"
27 #include "purify.h"
28 #include "interr.h"
29 #include "gc.h"
30 #include "gc-internal.h"
31 #include "primitive-objects.h"
33 #define PRINTNOISE
35 #if defined(__i386__)
36 /* again, what's so special about the x86 that this is differently
37 * visible there than on other platforms? -dan 20010125
39 static lispobj *dynamic_space_free_pointer;
40 #endif
42 #define gc_abort() \
43 lose("GC invariant lost, file \"%s\", line %d", __FILE__, __LINE__)
45 #if 1
46 #define gc_assert(ex) do { \
47 if (!(ex)) gc_abort(); \
48 } while (0)
49 #else
50 #define gc_assert(ex)
51 #endif
54 /* These hold the original end of the read_only and static spaces so
55 * we can tell what are forwarding pointers. */
57 static lispobj *read_only_end, *static_end;
59 static lispobj *read_only_free, *static_free;
61 static lispobj *pscav(lispobj *addr, int nwords, boolean constant);
63 #define LATERBLOCKSIZE 1020
64 #define LATERMAXCOUNT 10
66 static struct
67 later {
68 struct later *next;
69 union {
70 lispobj *ptr;
71 int count;
72 } u[LATERBLOCKSIZE];
73 } *later_blocks = NULL;
74 static int later_count = 0;
76 #define CEILING(x,y) (((x) + ((y) - 1)) & (~((y) - 1)))
77 #define NWORDS(x,y) (CEILING((x),(y)) / (y))
79 /* FIXME: Shouldn't this be defined in sbcl.h? See also notes in
80 * cheneygc.c */
82 #ifdef sparc
83 #define FUN_RAW_ADDR_OFFSET 0
84 #else
85 #define FUN_RAW_ADDR_OFFSET (6*sizeof(lispobj) - FUN_POINTER_LOWTAG)
86 #endif
88 static boolean
89 forwarding_pointer_p(lispobj obj)
91 lispobj *ptr = native_pointer(obj);
93 return ((static_end <= ptr && ptr <= static_free) ||
94 (read_only_end <= ptr && ptr <= read_only_free));
97 static boolean
98 dynamic_pointer_p(lispobj ptr)
100 #ifndef __i386__
101 return (ptr >= (lispobj)current_dynamic_space
103 ptr < (lispobj)dynamic_space_free_pointer);
104 #else
105 /* Be more conservative, and remember, this is a maybe. */
106 return (ptr >= (lispobj)DYNAMIC_SPACE_START
108 ptr < (lispobj)dynamic_space_free_pointer);
109 #endif
113 #ifdef __i386__
115 #ifdef LISP_FEATURE_GENCGC
117 * enhanced x86/GENCGC stack scavenging by Douglas Crosher
119 * Scavenging the stack on the i386 is problematic due to conservative
120 * roots and raw return addresses. Here it is handled in two passes:
121 * the first pass runs before any objects are moved and tries to
122 * identify valid pointers and return address on the stack, the second
123 * pass scavenges these.
126 static unsigned pointer_filter_verbose = 0;
128 /* FIXME: This is substantially the same code as in gencgc.c. (There
129 * are some differences, at least (1) the gencgc.c code needs to worry
130 * about return addresses on the stack pinning code objects, (2) the
131 * gencgc.c code needs to worry about the GC maybe happening in an
132 * interrupt service routine when the main thread of control was
133 * interrupted just as it had allocated memory and before it
134 * initialized it, while PURIFY needn't worry about that, and (3) the
135 * gencgc.c code has mutated more under maintenance since the fork
136 * from CMU CL than the code here has.) The two versions should be
137 * made to explicitly share common code, instead of just two different
138 * cut-and-pasted versions. */
139 static int
140 valid_dynamic_space_pointer(lispobj *pointer, lispobj *start_addr)
142 /* If it's not a return address then it needs to be a valid Lisp
143 * pointer. */
144 if (!is_lisp_pointer((lispobj)pointer))
145 return 0;
147 /* Check that the object pointed to is consistent with the pointer
148 * low tag. */
149 switch (lowtag_of((lispobj)pointer)) {
150 case FUN_POINTER_LOWTAG:
151 /* Start_addr should be the enclosing code object, or a closure
152 * header. */
153 switch (widetag_of(*start_addr)) {
154 case CODE_HEADER_WIDETAG:
155 /* This case is probably caught above. */
156 break;
157 case CLOSURE_HEADER_WIDETAG:
158 case FUNCALLABLE_INSTANCE_HEADER_WIDETAG:
159 if ((int)pointer != ((int)start_addr+FUN_POINTER_LOWTAG)) {
160 if (pointer_filter_verbose) {
161 fprintf(stderr,"*Wf2: %x %x %x\n", (unsigned int) pointer,
162 (unsigned int) start_addr, *start_addr);
164 return 0;
166 break;
167 default:
168 if (pointer_filter_verbose) {
169 fprintf(stderr,"*Wf3: %x %x %x\n", (unsigned int) pointer,
170 (unsigned int) start_addr, *start_addr);
172 return 0;
174 break;
175 case LIST_POINTER_LOWTAG:
176 if ((int)pointer != ((int)start_addr+LIST_POINTER_LOWTAG)) {
177 if (pointer_filter_verbose)
178 fprintf(stderr,"*Wl1: %x %x %x\n", (unsigned int) pointer,
179 (unsigned int) start_addr, *start_addr);
180 return 0;
182 /* Is it plausible cons? */
183 if ((is_lisp_pointer(start_addr[0])
184 || ((start_addr[0] & 3) == 0) /* fixnum */
185 || (widetag_of(start_addr[0]) == BASE_CHAR_WIDETAG)
186 || (widetag_of(start_addr[0]) == UNBOUND_MARKER_WIDETAG))
187 && (is_lisp_pointer(start_addr[1])
188 || ((start_addr[1] & 3) == 0) /* fixnum */
189 || (widetag_of(start_addr[1]) == BASE_CHAR_WIDETAG)
190 || (widetag_of(start_addr[1]) == UNBOUND_MARKER_WIDETAG))) {
191 break;
192 } else {
193 if (pointer_filter_verbose) {
194 fprintf(stderr,"*Wl2: %x %x %x\n", (unsigned int) pointer,
195 (unsigned int) start_addr, *start_addr);
197 return 0;
199 case INSTANCE_POINTER_LOWTAG:
200 if ((int)pointer != ((int)start_addr+INSTANCE_POINTER_LOWTAG)) {
201 if (pointer_filter_verbose) {
202 fprintf(stderr,"*Wi1: %x %x %x\n", (unsigned int) pointer,
203 (unsigned int) start_addr, *start_addr);
205 return 0;
207 if (widetag_of(start_addr[0]) != INSTANCE_HEADER_WIDETAG) {
208 if (pointer_filter_verbose) {
209 fprintf(stderr,"*Wi2: %x %x %x\n", (unsigned int) pointer,
210 (unsigned int) start_addr, *start_addr);
212 return 0;
214 break;
215 case OTHER_POINTER_LOWTAG:
216 if ((int)pointer != ((int)start_addr+OTHER_POINTER_LOWTAG)) {
217 if (pointer_filter_verbose) {
218 fprintf(stderr,"*Wo1: %x %x %x\n", (unsigned int) pointer,
219 (unsigned int) start_addr, *start_addr);
221 return 0;
223 /* Is it plausible? Not a cons. XXX should check the headers. */
224 if (is_lisp_pointer(start_addr[0]) || ((start_addr[0] & 3) == 0)) {
225 if (pointer_filter_verbose) {
226 fprintf(stderr,"*Wo2: %x %x %x\n", (unsigned int) pointer,
227 (unsigned int) start_addr, *start_addr);
229 return 0;
231 switch (widetag_of(start_addr[0])) {
232 case UNBOUND_MARKER_WIDETAG:
233 case BASE_CHAR_WIDETAG:
234 if (pointer_filter_verbose) {
235 fprintf(stderr,"*Wo3: %x %x %x\n", (unsigned int) pointer,
236 (unsigned int) start_addr, *start_addr);
238 return 0;
240 /* only pointed to by function pointers? */
241 case CLOSURE_HEADER_WIDETAG:
242 case FUNCALLABLE_INSTANCE_HEADER_WIDETAG:
243 if (pointer_filter_verbose) {
244 fprintf(stderr,"*Wo4: %x %x %x\n", (unsigned int) pointer,
245 (unsigned int) start_addr, *start_addr);
247 return 0;
249 case INSTANCE_HEADER_WIDETAG:
250 if (pointer_filter_verbose) {
251 fprintf(stderr,"*Wo5: %x %x %x\n", (unsigned int) pointer,
252 (unsigned int) start_addr, *start_addr);
254 return 0;
256 /* the valid other immediate pointer objects */
257 case SIMPLE_VECTOR_WIDETAG:
258 case RATIO_WIDETAG:
259 case COMPLEX_WIDETAG:
260 #ifdef COMPLEX_SINGLE_FLOAT_WIDETAG
261 case COMPLEX_SINGLE_FLOAT_WIDETAG:
262 #endif
263 #ifdef COMPLEX_DOUBLE_FLOAT_WIDETAG
264 case COMPLEX_DOUBLE_FLOAT_WIDETAG:
265 #endif
266 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
267 case COMPLEX_LONG_FLOAT_WIDETAG:
268 #endif
269 case SIMPLE_ARRAY_WIDETAG:
270 case COMPLEX_STRING_WIDETAG:
271 case COMPLEX_BIT_VECTOR_WIDETAG:
272 case COMPLEX_VECTOR_WIDETAG:
273 case COMPLEX_ARRAY_WIDETAG:
274 case VALUE_CELL_HEADER_WIDETAG:
275 case SYMBOL_HEADER_WIDETAG:
276 case FDEFN_WIDETAG:
277 case CODE_HEADER_WIDETAG:
278 case BIGNUM_WIDETAG:
279 case SINGLE_FLOAT_WIDETAG:
280 case DOUBLE_FLOAT_WIDETAG:
281 #ifdef LONG_FLOAT_WIDETAG
282 case LONG_FLOAT_WIDETAG:
283 #endif
284 case SIMPLE_STRING_WIDETAG:
285 case SIMPLE_BIT_VECTOR_WIDETAG:
286 case SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG:
287 case SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG:
288 case SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG:
289 case SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG:
290 case SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG:
291 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
292 case SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG:
293 #endif
294 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
295 case SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG:
296 #endif
297 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG
298 case SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG:
299 #endif
300 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
301 case SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG:
302 #endif
303 case SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG:
304 case SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG:
305 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
306 case SIMPLE_ARRAY_LONG_FLOAT_WIDETAG:
307 #endif
308 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
309 case SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG:
310 #endif
311 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
312 case SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG:
313 #endif
314 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
315 case SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG:
316 #endif
317 case SAP_WIDETAG:
318 case WEAK_POINTER_WIDETAG:
319 break;
321 default:
322 if (pointer_filter_verbose) {
323 fprintf(stderr,"*Wo6: %x %x %x\n", (unsigned int) pointer,
324 (unsigned int) start_addr, *start_addr);
326 return 0;
328 break;
329 default:
330 if (pointer_filter_verbose) {
331 fprintf(stderr,"*W?: %x %x %x\n", (unsigned int) pointer,
332 (unsigned int) start_addr, *start_addr);
334 return 0;
337 /* looks good */
338 return 1;
341 #define MAX_STACK_POINTERS 256
342 lispobj *valid_stack_locations[MAX_STACK_POINTERS];
343 unsigned int num_valid_stack_locations;
345 #define MAX_STACK_RETURN_ADDRESSES 128
346 lispobj *valid_stack_ra_locations[MAX_STACK_RETURN_ADDRESSES];
347 lispobj *valid_stack_ra_code_objects[MAX_STACK_RETURN_ADDRESSES];
348 unsigned int num_valid_stack_ra_locations;
350 /* Identify valid stack slots. */
351 static void
352 setup_i386_stack_scav(lispobj *lowaddr, lispobj *base)
354 lispobj *sp = lowaddr;
355 num_valid_stack_locations = 0;
356 num_valid_stack_ra_locations = 0;
357 for (sp = lowaddr; sp < base; sp++) {
358 lispobj thing = *sp;
359 /* Find the object start address */
360 lispobj *start_addr = search_dynamic_space((void *)thing);
361 if (start_addr) {
362 /* We need to allow raw pointers into Code objects for
363 * return addresses. This will also pick up pointers to
364 * functions in code objects. */
365 if (widetag_of(*start_addr) == CODE_HEADER_WIDETAG) {
366 /* FIXME asserting here is a really dumb thing to do.
367 * If we've overflowed some arbitrary static limit, we
368 * should just refuse to purify, instead of killing
369 * the whole lisp session
371 gc_assert(num_valid_stack_ra_locations <
372 MAX_STACK_RETURN_ADDRESSES);
373 valid_stack_ra_locations[num_valid_stack_ra_locations] = sp;
374 valid_stack_ra_code_objects[num_valid_stack_ra_locations++] =
375 (lispobj *)((int)start_addr + OTHER_POINTER_LOWTAG);
376 } else {
377 if (valid_dynamic_space_pointer((void *)thing, start_addr)) {
378 gc_assert(num_valid_stack_locations < MAX_STACK_POINTERS);
379 valid_stack_locations[num_valid_stack_locations++] = sp;
384 if (pointer_filter_verbose) {
385 fprintf(stderr, "number of valid stack pointers = %d\n",
386 num_valid_stack_locations);
387 fprintf(stderr, "number of stack return addresses = %d\n",
388 num_valid_stack_ra_locations);
392 static void
393 pscav_i386_stack(void)
395 int i;
397 for (i = 0; i < num_valid_stack_locations; i++)
398 pscav(valid_stack_locations[i], 1, 0);
400 for (i = 0; i < num_valid_stack_ra_locations; i++) {
401 lispobj code_obj = (lispobj)valid_stack_ra_code_objects[i];
402 pscav(&code_obj, 1, 0);
403 if (pointer_filter_verbose) {
404 fprintf(stderr,"*C moved RA %x to %x; for code object %x to %x\n",
405 *valid_stack_ra_locations[i],
406 (int)(*valid_stack_ra_locations[i])
407 - ((int)valid_stack_ra_code_objects[i] - (int)code_obj),
408 (unsigned int) valid_stack_ra_code_objects[i], code_obj);
410 *valid_stack_ra_locations[i] =
411 ((int)(*valid_stack_ra_locations[i])
412 - ((int)valid_stack_ra_code_objects[i] - (int)code_obj));
415 #endif
416 #endif
419 static void
420 pscav_later(lispobj *where, int count)
422 struct later *new;
424 if (count > LATERMAXCOUNT) {
425 while (count > LATERMAXCOUNT) {
426 pscav_later(where, LATERMAXCOUNT);
427 count -= LATERMAXCOUNT;
428 where += LATERMAXCOUNT;
431 else {
432 if (later_blocks == NULL || later_count == LATERBLOCKSIZE ||
433 (later_count == LATERBLOCKSIZE-1 && count > 1)) {
434 new = (struct later *)malloc(sizeof(struct later));
435 new->next = later_blocks;
436 if (later_blocks && later_count < LATERBLOCKSIZE)
437 later_blocks->u[later_count].ptr = NULL;
438 later_blocks = new;
439 later_count = 0;
442 if (count != 1)
443 later_blocks->u[later_count++].count = count;
444 later_blocks->u[later_count++].ptr = where;
448 static lispobj
449 ptrans_boxed(lispobj thing, lispobj header, boolean constant)
451 int nwords;
452 lispobj result, *new, *old;
454 nwords = 1 + HeaderValue(header);
456 /* Allocate it */
457 old = (lispobj *)native_pointer(thing);
458 if (constant) {
459 new = read_only_free;
460 read_only_free += CEILING(nwords, 2);
462 else {
463 new = static_free;
464 static_free += CEILING(nwords, 2);
467 /* Copy it. */
468 bcopy(old, new, nwords * sizeof(lispobj));
470 /* Deposit forwarding pointer. */
471 result = make_lispobj(new, lowtag_of(thing));
472 *old = result;
474 /* Scavenge it. */
475 pscav(new, nwords, constant);
477 return result;
480 /* We need to look at the layout to see whether it is a pure structure
481 * class, and only then can we transport as constant. If it is pure,
482 * we can ALWAYS transport as a constant. */
483 static lispobj
484 ptrans_instance(lispobj thing, lispobj header, boolean constant)
486 lispobj layout = ((struct instance *)native_pointer(thing))->slots[0];
487 lispobj pure = ((struct instance *)native_pointer(layout))->slots[15];
489 switch (pure) {
490 case T:
491 return (ptrans_boxed(thing, header, 1));
492 case NIL:
493 return (ptrans_boxed(thing, header, 0));
494 case 0:
496 /* Substructure: special case for the COMPACT-INFO-ENVs,
497 * where the instance may have a point to the dynamic
498 * space placed into it (e.g. the cache-name slot), but
499 * the lists and arrays at the time of a purify can be
500 * moved to the RO space. */
501 int nwords;
502 lispobj result, *new, *old;
504 nwords = 1 + HeaderValue(header);
506 /* Allocate it */
507 old = (lispobj *)native_pointer(thing);
508 new = static_free;
509 static_free += CEILING(nwords, 2);
511 /* Copy it. */
512 bcopy(old, new, nwords * sizeof(lispobj));
514 /* Deposit forwarding pointer. */
515 result = make_lispobj(new, lowtag_of(thing));
516 *old = result;
518 /* Scavenge it. */
519 pscav(new, nwords, 1);
521 return result;
523 default:
524 gc_abort();
525 return NIL; /* dummy value: return something ... */
529 static lispobj
530 ptrans_fdefn(lispobj thing, lispobj header)
532 int nwords;
533 lispobj result, *new, *old, oldfn;
534 struct fdefn *fdefn;
536 nwords = 1 + HeaderValue(header);
538 /* Allocate it */
539 old = (lispobj *)native_pointer(thing);
540 new = static_free;
541 static_free += CEILING(nwords, 2);
543 /* Copy it. */
544 bcopy(old, new, nwords * sizeof(lispobj));
546 /* Deposit forwarding pointer. */
547 result = make_lispobj(new, lowtag_of(thing));
548 *old = result;
550 /* Scavenge the function. */
551 fdefn = (struct fdefn *)new;
552 oldfn = fdefn->fun;
553 pscav(&fdefn->fun, 1, 0);
554 if ((char *)oldfn + FUN_RAW_ADDR_OFFSET == fdefn->raw_addr)
555 fdefn->raw_addr = (char *)fdefn->fun + FUN_RAW_ADDR_OFFSET;
557 return result;
560 static lispobj
561 ptrans_unboxed(lispobj thing, lispobj header)
563 int nwords;
564 lispobj result, *new, *old;
566 nwords = 1 + HeaderValue(header);
568 /* Allocate it */
569 old = (lispobj *)native_pointer(thing);
570 new = read_only_free;
571 read_only_free += CEILING(nwords, 2);
573 /* Copy it. */
574 bcopy(old, new, nwords * sizeof(lispobj));
576 /* Deposit forwarding pointer. */
577 result = make_lispobj(new , lowtag_of(thing));
578 *old = result;
580 return result;
583 static lispobj
584 ptrans_vector(lispobj thing, int bits, int extra,
585 boolean boxed, boolean constant)
587 struct vector *vector;
588 int nwords;
589 lispobj result, *new;
591 vector = (struct vector *)native_pointer(thing);
592 nwords = 2 + (CEILING((fixnum_value(vector->length)+extra)*bits,32)>>5);
594 if (boxed && !constant) {
595 new = static_free;
596 static_free += CEILING(nwords, 2);
598 else {
599 new = read_only_free;
600 read_only_free += CEILING(nwords, 2);
603 bcopy(vector, new, nwords * sizeof(lispobj));
605 result = make_lispobj(new, lowtag_of(thing));
606 vector->header = result;
608 if (boxed)
609 pscav(new, nwords, constant);
611 return result;
614 #ifdef __i386__
615 static void
616 apply_code_fixups_during_purify(struct code *old_code, struct code *new_code)
618 int nheader_words, ncode_words, nwords;
619 void *constants_start_addr, *constants_end_addr;
620 void *code_start_addr, *code_end_addr;
621 lispobj fixups = NIL;
622 unsigned displacement = (unsigned)new_code - (unsigned)old_code;
623 struct vector *fixups_vector;
625 ncode_words = fixnum_value(new_code->code_size);
626 nheader_words = HeaderValue(*(lispobj *)new_code);
627 nwords = ncode_words + nheader_words;
629 constants_start_addr = (void *)new_code + 5*4;
630 constants_end_addr = (void *)new_code + nheader_words*4;
631 code_start_addr = (void *)new_code + nheader_words*4;
632 code_end_addr = (void *)new_code + nwords*4;
634 /* The first constant should be a pointer to the fixups for this
635 * code objects. Check. */
636 fixups = new_code->constants[0];
638 /* It will be 0 or the unbound-marker if there are no fixups, and
639 * will be an other-pointer to a vector if it is valid. */
640 if ((fixups==0) ||
641 (fixups==UNBOUND_MARKER_WIDETAG) ||
642 !is_lisp_pointer(fixups)) {
643 #ifdef LISP_FEATURE_GENCGC
644 /* Check for a possible errors. */
645 sniff_code_object(new_code,displacement);
646 #endif
647 return;
650 fixups_vector = (struct vector *)native_pointer(fixups);
652 /* Could be pointing to a forwarding pointer. */
653 if (is_lisp_pointer(fixups) && (dynamic_pointer_p(fixups))
654 && forwarding_pointer_p(*(lispobj *)fixups_vector)) {
655 /* If so then follow it. */
656 fixups_vector =
657 (struct vector *)native_pointer(*(lispobj *)fixups_vector);
660 if (widetag_of(fixups_vector->header) ==
661 SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG) {
662 /* We got the fixups for the code block. Now work through the
663 * vector, and apply a fixup at each address. */
664 int length = fixnum_value(fixups_vector->length);
665 int i;
666 for (i=0; i<length; i++) {
667 unsigned offset = fixups_vector->data[i];
668 /* Now check the current value of offset. */
669 unsigned old_value =
670 *(unsigned *)((unsigned)code_start_addr + offset);
672 /* If it's within the old_code object then it must be an
673 * absolute fixup (relative ones are not saved) */
674 if ((old_value>=(unsigned)old_code)
675 && (old_value<((unsigned)old_code + nwords*4)))
676 /* So add the dispacement. */
677 *(unsigned *)((unsigned)code_start_addr + offset) = old_value
678 + displacement;
679 else
680 /* It is outside the old code object so it must be a relative
681 * fixup (absolute fixups are not saved). So subtract the
682 * displacement. */
683 *(unsigned *)((unsigned)code_start_addr + offset) = old_value
684 - displacement;
688 /* No longer need the fixups. */
689 new_code->constants[0] = 0;
691 #ifdef LISP_FEATURE_GENCGC
692 /* Check for possible errors. */
693 sniff_code_object(new_code,displacement);
694 #endif
696 #endif
698 static lispobj
699 ptrans_code(lispobj thing)
701 struct code *code, *new;
702 int nwords;
703 lispobj func, result;
705 code = (struct code *)native_pointer(thing);
706 nwords = HeaderValue(code->header) + fixnum_value(code->code_size);
708 new = (struct code *)read_only_free;
709 read_only_free += CEILING(nwords, 2);
711 bcopy(code, new, nwords * sizeof(lispobj));
713 #ifdef LISP_FEATURE_X86
714 apply_code_fixups_during_purify(code,new);
715 #endif
717 result = make_lispobj(new, OTHER_POINTER_LOWTAG);
719 /* Stick in a forwarding pointer for the code object. */
720 *(lispobj *)code = result;
722 /* Put in forwarding pointers for all the functions. */
723 for (func = code->entry_points;
724 func != NIL;
725 func = ((struct simple_fun *)native_pointer(func))->next) {
727 gc_assert(lowtag_of(func) == FUN_POINTER_LOWTAG);
729 *(lispobj *)native_pointer(func) = result + (func - thing);
732 /* Arrange to scavenge the debug info later. */
733 pscav_later(&new->debug_info, 1);
735 if (new->trace_table_offset & 0x3)
736 #if 0
737 pscav(&new->trace_table_offset, 1, 0);
738 #else
739 new->trace_table_offset = NIL; /* limit lifetime */
740 #endif
742 /* Scavenge the constants. */
743 pscav(new->constants, HeaderValue(new->header)-5, 1);
745 /* Scavenge all the functions. */
746 pscav(&new->entry_points, 1, 1);
747 for (func = new->entry_points;
748 func != NIL;
749 func = ((struct simple_fun *)native_pointer(func))->next) {
750 gc_assert(lowtag_of(func) == FUN_POINTER_LOWTAG);
751 gc_assert(!dynamic_pointer_p(func));
753 #ifdef __i386__
754 /* Temporarly convert the self pointer to a real function pointer. */
755 ((struct simple_fun *)native_pointer(func))->self
756 -= FUN_RAW_ADDR_OFFSET;
757 #endif
758 pscav(&((struct simple_fun *)native_pointer(func))->self, 2, 1);
759 #ifdef __i386__
760 ((struct simple_fun *)native_pointer(func))->self
761 += FUN_RAW_ADDR_OFFSET;
762 #endif
763 pscav_later(&((struct simple_fun *)native_pointer(func))->name, 3);
766 return result;
769 static lispobj
770 ptrans_func(lispobj thing, lispobj header)
772 int nwords;
773 lispobj code, *new, *old, result;
774 struct simple_fun *function;
776 /* Thing can either be a function header, a closure function
777 * header, a closure, or a funcallable-instance. If it's a closure
778 * or a funcallable-instance, we do the same as ptrans_boxed.
779 * Otherwise we have to do something strange, 'cause it is buried
780 * inside a code object. */
782 if (widetag_of(header) == SIMPLE_FUN_HEADER_WIDETAG ||
783 widetag_of(header) == CLOSURE_FUN_HEADER_WIDETAG) {
785 /* We can only end up here if the code object has not been
786 * scavenged, because if it had been scavenged, forwarding pointers
787 * would have been left behind for all the entry points. */
789 function = (struct simple_fun *)native_pointer(thing);
790 code =
791 make_lispobj
792 ((native_pointer(thing) -
793 (HeaderValue(function->header))), OTHER_POINTER_LOWTAG);
795 /* This will cause the function's header to be replaced with a
796 * forwarding pointer. */
798 ptrans_code(code);
800 /* So we can just return that. */
801 return function->header;
803 else {
804 /* It's some kind of closure-like thing. */
805 nwords = 1 + HeaderValue(header);
806 old = (lispobj *)native_pointer(thing);
808 /* Allocate the new one. */
809 if (widetag_of(header) == FUNCALLABLE_INSTANCE_HEADER_WIDETAG) {
810 /* FINs *must* not go in read_only space. */
811 new = static_free;
812 static_free += CEILING(nwords, 2);
814 else {
815 /* Closures can always go in read-only space, 'cause they
816 * never change. */
818 new = read_only_free;
819 read_only_free += CEILING(nwords, 2);
821 /* Copy it. */
822 bcopy(old, new, nwords * sizeof(lispobj));
824 /* Deposit forwarding pointer. */
825 result = make_lispobj(new, lowtag_of(thing));
826 *old = result;
828 /* Scavenge it. */
829 pscav(new, nwords, 0);
831 return result;
835 static lispobj
836 ptrans_returnpc(lispobj thing, lispobj header)
838 lispobj code, new;
840 /* Find the corresponding code object. */
841 code = thing - HeaderValue(header)*sizeof(lispobj);
843 /* Make sure it's been transported. */
844 new = *(lispobj *)native_pointer(code);
845 if (!forwarding_pointer_p(new))
846 new = ptrans_code(code);
848 /* Maintain the offset: */
849 return new + (thing - code);
852 #define WORDS_PER_CONS CEILING(sizeof(struct cons) / sizeof(lispobj), 2)
854 static lispobj
855 ptrans_list(lispobj thing, boolean constant)
857 struct cons *old, *new, *orig;
858 int length;
860 if (constant)
861 orig = (struct cons *)read_only_free;
862 else
863 orig = (struct cons *)static_free;
864 length = 0;
866 do {
867 /* Allocate a new cons cell. */
868 old = (struct cons *)native_pointer(thing);
869 if (constant) {
870 new = (struct cons *)read_only_free;
871 read_only_free += WORDS_PER_CONS;
873 else {
874 new = (struct cons *)static_free;
875 static_free += WORDS_PER_CONS;
878 /* Copy the cons cell and keep a pointer to the cdr. */
879 new->car = old->car;
880 thing = new->cdr = old->cdr;
882 /* Set up the forwarding pointer. */
883 *(lispobj *)old = make_lispobj(new, LIST_POINTER_LOWTAG);
885 /* And count this cell. */
886 length++;
887 } while (lowtag_of(thing) == LIST_POINTER_LOWTAG &&
888 dynamic_pointer_p(thing) &&
889 !(forwarding_pointer_p(*(lispobj *)native_pointer(thing))));
891 /* Scavenge the list we just copied. */
892 pscav((lispobj *)orig, length * WORDS_PER_CONS, constant);
894 return make_lispobj(orig, LIST_POINTER_LOWTAG);
897 static lispobj
898 ptrans_otherptr(lispobj thing, lispobj header, boolean constant)
900 switch (widetag_of(header)) {
901 case BIGNUM_WIDETAG:
902 case SINGLE_FLOAT_WIDETAG:
903 case DOUBLE_FLOAT_WIDETAG:
904 #ifdef LONG_FLOAT_WIDETAG
905 case LONG_FLOAT_WIDETAG:
906 #endif
907 #ifdef COMPLEX_SINGLE_FLOAT_WIDETAG
908 case COMPLEX_SINGLE_FLOAT_WIDETAG:
909 #endif
910 #ifdef COMPLEX_DOUBLE_FLOAT_WIDETAG
911 case COMPLEX_DOUBLE_FLOAT_WIDETAG:
912 #endif
913 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
914 case COMPLEX_LONG_FLOAT_WIDETAG:
915 #endif
916 case SAP_WIDETAG:
917 return ptrans_unboxed(thing, header);
919 case RATIO_WIDETAG:
920 case COMPLEX_WIDETAG:
921 case SIMPLE_ARRAY_WIDETAG:
922 case COMPLEX_STRING_WIDETAG:
923 case COMPLEX_VECTOR_WIDETAG:
924 case COMPLEX_ARRAY_WIDETAG:
925 return ptrans_boxed(thing, header, constant);
927 case VALUE_CELL_HEADER_WIDETAG:
928 case WEAK_POINTER_WIDETAG:
929 return ptrans_boxed(thing, header, 0);
931 case SYMBOL_HEADER_WIDETAG:
932 return ptrans_boxed(thing, header, 0);
934 case SIMPLE_STRING_WIDETAG:
935 return ptrans_vector(thing, 8, 1, 0, constant);
937 case SIMPLE_BIT_VECTOR_WIDETAG:
938 return ptrans_vector(thing, 1, 0, 0, constant);
940 case SIMPLE_VECTOR_WIDETAG:
941 return ptrans_vector(thing, 32, 0, 1, constant);
943 case SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG:
944 return ptrans_vector(thing, 2, 0, 0, constant);
946 case SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG:
947 return ptrans_vector(thing, 4, 0, 0, constant);
949 case SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG:
950 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
951 case SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG:
952 #endif
953 return ptrans_vector(thing, 8, 0, 0, constant);
955 case SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG:
956 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
957 case SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG:
958 #endif
959 return ptrans_vector(thing, 16, 0, 0, constant);
961 case SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG:
962 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG
963 case SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG:
964 #endif
965 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
966 case SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG:
967 #endif
968 return ptrans_vector(thing, 32, 0, 0, constant);
970 case SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG:
971 return ptrans_vector(thing, 32, 0, 0, constant);
973 case SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG:
974 return ptrans_vector(thing, 64, 0, 0, constant);
976 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
977 case SIMPLE_ARRAY_LONG_FLOAT_WIDETAG:
978 #ifdef __i386__
979 return ptrans_vector(thing, 96, 0, 0, constant);
980 #endif
981 #ifdef sparc
982 return ptrans_vector(thing, 128, 0, 0, constant);
983 #endif
984 #endif
986 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
987 case SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG:
988 return ptrans_vector(thing, 64, 0, 0, constant);
989 #endif
991 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
992 case SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG:
993 return ptrans_vector(thing, 128, 0, 0, constant);
994 #endif
996 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
997 case SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG:
998 #ifdef __i386__
999 return ptrans_vector(thing, 192, 0, 0, constant);
1000 #endif
1001 #ifdef sparc
1002 return ptrans_vector(thing, 256, 0, 0, constant);
1003 #endif
1004 #endif
1006 case CODE_HEADER_WIDETAG:
1007 return ptrans_code(thing);
1009 case RETURN_PC_HEADER_WIDETAG:
1010 return ptrans_returnpc(thing, header);
1012 case FDEFN_WIDETAG:
1013 return ptrans_fdefn(thing, header);
1015 default:
1016 /* Should only come across other pointers to the above stuff. */
1017 gc_abort();
1018 return NIL;
1022 static int
1023 pscav_fdefn(struct fdefn *fdefn)
1025 boolean fix_func;
1027 fix_func = ((char *)(fdefn->fun+FUN_RAW_ADDR_OFFSET) == fdefn->raw_addr);
1028 pscav(&fdefn->name, 1, 1);
1029 pscav(&fdefn->fun, 1, 0);
1030 if (fix_func)
1031 fdefn->raw_addr = (char *)(fdefn->fun + FUN_RAW_ADDR_OFFSET);
1032 return sizeof(struct fdefn) / sizeof(lispobj);
1035 #ifdef __i386__
1036 /* now putting code objects in static space */
1037 static int
1038 pscav_code(struct code*code)
1040 int nwords;
1041 lispobj func;
1042 nwords = HeaderValue(code->header) + fixnum_value(code->code_size);
1044 /* Arrange to scavenge the debug info later. */
1045 pscav_later(&code->debug_info, 1);
1047 /* Scavenge the constants. */
1048 pscav(code->constants, HeaderValue(code->header)-5, 1);
1050 /* Scavenge all the functions. */
1051 pscav(&code->entry_points, 1, 1);
1052 for (func = code->entry_points;
1053 func != NIL;
1054 func = ((struct simple_fun *)native_pointer(func))->next) {
1055 gc_assert(lowtag_of(func) == FUN_POINTER_LOWTAG);
1056 gc_assert(!dynamic_pointer_p(func));
1058 #ifdef __i386__
1059 /* Temporarly convert the self pointer to a real function
1060 * pointer. */
1061 ((struct simple_fun *)native_pointer(func))->self
1062 -= FUN_RAW_ADDR_OFFSET;
1063 #endif
1064 pscav(&((struct simple_fun *)native_pointer(func))->self, 2, 1);
1065 #ifdef __i386__
1066 ((struct simple_fun *)native_pointer(func))->self
1067 += FUN_RAW_ADDR_OFFSET;
1068 #endif
1069 pscav_later(&((struct simple_fun *)native_pointer(func))->name, 3);
1072 return CEILING(nwords,2);
1074 #endif
1076 static lispobj *
1077 pscav(lispobj *addr, int nwords, boolean constant)
1079 lispobj thing, *thingp, header;
1080 int count = 0; /* (0 = dummy init value to stop GCC warning) */
1081 struct vector *vector;
1083 while (nwords > 0) {
1084 thing = *addr;
1085 if (is_lisp_pointer(thing)) {
1086 /* It's a pointer. Is it something we might have to move? */
1087 if (dynamic_pointer_p(thing)) {
1088 /* Maybe. Have we already moved it? */
1089 thingp = (lispobj *)native_pointer(thing);
1090 header = *thingp;
1091 if (is_lisp_pointer(header) && forwarding_pointer_p(header))
1092 /* Yep, so just copy the forwarding pointer. */
1093 thing = header;
1094 else {
1095 /* Nope, copy the object. */
1096 switch (lowtag_of(thing)) {
1097 case FUN_POINTER_LOWTAG:
1098 thing = ptrans_func(thing, header);
1099 break;
1101 case LIST_POINTER_LOWTAG:
1102 thing = ptrans_list(thing, constant);
1103 break;
1105 case INSTANCE_POINTER_LOWTAG:
1106 thing = ptrans_instance(thing, header, constant);
1107 break;
1109 case OTHER_POINTER_LOWTAG:
1110 thing = ptrans_otherptr(thing, header, constant);
1111 break;
1113 default:
1114 /* It was a pointer, but not one of them? */
1115 gc_abort();
1118 *addr = thing;
1120 count = 1;
1122 else if (thing & 3) {
1123 /* It's an other immediate. Maybe the header for an unboxed */
1124 /* object. */
1125 switch (widetag_of(thing)) {
1126 case BIGNUM_WIDETAG:
1127 case SINGLE_FLOAT_WIDETAG:
1128 case DOUBLE_FLOAT_WIDETAG:
1129 #ifdef LONG_FLOAT_WIDETAG
1130 case LONG_FLOAT_WIDETAG:
1131 #endif
1132 case SAP_WIDETAG:
1133 /* It's an unboxed simple object. */
1134 count = HeaderValue(thing)+1;
1135 break;
1137 case SIMPLE_VECTOR_WIDETAG:
1138 if (HeaderValue(thing) == subtype_VectorValidHashing) {
1139 *addr = (subtype_VectorMustRehash << N_WIDETAG_BITS) |
1140 SIMPLE_VECTOR_WIDETAG;
1142 count = 1;
1143 break;
1145 case SIMPLE_STRING_WIDETAG:
1146 vector = (struct vector *)addr;
1147 count = CEILING(NWORDS(fixnum_value(vector->length)+1,4)+2,2);
1148 break;
1150 case SIMPLE_BIT_VECTOR_WIDETAG:
1151 vector = (struct vector *)addr;
1152 count = CEILING(NWORDS(fixnum_value(vector->length),32)+2,2);
1153 break;
1155 case SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG:
1156 vector = (struct vector *)addr;
1157 count = CEILING(NWORDS(fixnum_value(vector->length),16)+2,2);
1158 break;
1160 case SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG:
1161 vector = (struct vector *)addr;
1162 count = CEILING(NWORDS(fixnum_value(vector->length),8)+2,2);
1163 break;
1165 case SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG:
1166 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
1167 case SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG:
1168 #endif
1169 vector = (struct vector *)addr;
1170 count = CEILING(NWORDS(fixnum_value(vector->length),4)+2,2);
1171 break;
1173 case SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG:
1174 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
1175 case SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG:
1176 #endif
1177 vector = (struct vector *)addr;
1178 count = CEILING(NWORDS(fixnum_value(vector->length),2)+2,2);
1179 break;
1181 case SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG:
1182 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG
1183 case SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG:
1184 #endif
1185 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
1186 case SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG:
1187 #endif
1188 vector = (struct vector *)addr;
1189 count = CEILING(fixnum_value(vector->length)+2,2);
1190 break;
1192 case SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG:
1193 vector = (struct vector *)addr;
1194 count = CEILING(fixnum_value(vector->length)+2,2);
1195 break;
1197 case SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG:
1198 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
1199 case SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG:
1200 #endif
1201 vector = (struct vector *)addr;
1202 count = fixnum_value(vector->length)*2+2;
1203 break;
1205 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
1206 case SIMPLE_ARRAY_LONG_FLOAT_WIDETAG:
1207 vector = (struct vector *)addr;
1208 #ifdef __i386__
1209 count = fixnum_value(vector->length)*3+2;
1210 #endif
1211 #ifdef sparc
1212 count = fixnum_value(vector->length)*4+2;
1213 #endif
1214 break;
1215 #endif
1217 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
1218 case SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG:
1219 vector = (struct vector *)addr;
1220 count = fixnum_value(vector->length)*4+2;
1221 break;
1222 #endif
1224 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
1225 case SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG:
1226 vector = (struct vector *)addr;
1227 #ifdef __i386__
1228 count = fixnum_value(vector->length)*6+2;
1229 #endif
1230 #ifdef sparc
1231 count = fixnum_value(vector->length)*8+2;
1232 #endif
1233 break;
1234 #endif
1236 case CODE_HEADER_WIDETAG:
1237 #ifndef __i386__
1238 gc_abort(); /* no code headers in static space */
1239 #else
1240 count = pscav_code((struct code*)addr);
1241 #endif
1242 break;
1244 case SIMPLE_FUN_HEADER_WIDETAG:
1245 case CLOSURE_FUN_HEADER_WIDETAG:
1246 case RETURN_PC_HEADER_WIDETAG:
1247 /* We should never hit any of these, 'cause they occur
1248 * buried in the middle of code objects. */
1249 gc_abort();
1250 break;
1252 #ifdef __i386__
1253 case CLOSURE_HEADER_WIDETAG:
1254 case FUNCALLABLE_INSTANCE_HEADER_WIDETAG:
1255 /* The function self pointer needs special care on the
1256 * x86 because it is the real entry point. */
1258 lispobj fun = ((struct closure *)addr)->fun
1259 - FUN_RAW_ADDR_OFFSET;
1260 pscav(&fun, 1, constant);
1261 ((struct closure *)addr)->fun = fun + FUN_RAW_ADDR_OFFSET;
1263 count = 2;
1264 break;
1265 #endif
1267 case WEAK_POINTER_WIDETAG:
1268 /* Weak pointers get preserved during purify, 'cause I
1269 * don't feel like figuring out how to break them. */
1270 pscav(addr+1, 2, constant);
1271 count = 4;
1272 break;
1274 case FDEFN_WIDETAG:
1275 /* We have to handle fdefn objects specially, so we
1276 * can fix up the raw function address. */
1277 count = pscav_fdefn((struct fdefn *)addr);
1278 break;
1280 default:
1281 count = 1;
1282 break;
1285 else {
1286 /* It's a fixnum. */
1287 count = 1;
1290 addr += count;
1291 nwords -= count;
1294 return addr;
1298 purify(lispobj static_roots, lispobj read_only_roots)
1300 lispobj *clean;
1301 int count, i;
1302 struct later *laters, *next;
1305 #ifdef PRINTNOISE
1306 printf("[doing purification:");
1307 fflush(stdout);
1308 #endif
1309 #ifdef LISP_FEATURE_GENCGC
1310 gc_alloc_update_all_page_tables();
1311 #endif
1312 if (fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX)) != 0) {
1313 /* FIXME: 1. What does this mean? 2. It shouldn't be reporting
1314 * its error simply by a. printing a string b. to stdout instead
1315 * of stderr. */
1316 printf(" Ack! Can't purify interrupt contexts. ");
1317 fflush(stdout);
1318 return 0;
1321 #if defined(__i386__)
1322 dynamic_space_free_pointer =
1323 (lispobj*)SymbolValue(ALLOCATION_POINTER);
1324 #endif
1326 read_only_end = read_only_free =
1327 (lispobj *)SymbolValue(READ_ONLY_SPACE_FREE_POINTER);
1328 static_end = static_free =
1329 (lispobj *)SymbolValue(STATIC_SPACE_FREE_POINTER);
1331 #ifdef PRINTNOISE
1332 printf(" roots");
1333 fflush(stdout);
1334 #endif
1336 #if (defined(LISP_FEATURE_GENCGC) && defined(LISP_FEATURE_X86))
1337 gc_assert((lispobj *)CONTROL_STACK_END > ((&read_only_roots)+1));
1338 setup_i386_stack_scav(((&static_roots)-2), (lispobj *)CONTROL_STACK_END);
1339 #endif
1341 pscav(&static_roots, 1, 0);
1342 pscav(&read_only_roots, 1, 1);
1344 #ifdef PRINTNOISE
1345 printf(" handlers");
1346 fflush(stdout);
1347 #endif
1348 pscav((lispobj *) interrupt_handlers,
1349 sizeof(interrupt_handlers) / sizeof(lispobj),
1352 #ifdef PRINTNOISE
1353 printf(" stack");
1354 fflush(stdout);
1355 #endif
1356 #ifndef __i386__
1357 pscav((lispobj *)CONTROL_STACK_START,
1358 current_control_stack_pointer - (lispobj *)CONTROL_STACK_START,
1360 #else
1361 #ifdef LISP_FEATURE_GENCGC
1362 pscav_i386_stack();
1363 #endif
1364 #endif
1366 #ifdef PRINTNOISE
1367 printf(" bindings");
1368 fflush(stdout);
1369 #endif
1370 #if !defined(__i386__)
1371 pscav( (lispobj *)BINDING_STACK_START,
1372 (lispobj *)current_binding_stack_pointer - (lispobj *)BINDING_STACK_START,
1374 #else
1375 pscav( (lispobj *)BINDING_STACK_START,
1376 (lispobj *)SymbolValue(BINDING_STACK_POINTER) -
1377 (lispobj *)BINDING_STACK_START,
1379 #endif
1381 /* The original CMU CL code had scavenge-read-only-space code
1382 * controlled by the Lisp-level variable
1383 * *SCAVENGE-READ-ONLY-SPACE*. It was disabled by default, and it
1384 * wasn't documented under what circumstances it was useful or
1385 * safe to turn it on, so it's been turned off in SBCL. If you
1386 * want/need this functionality, and can test and document it,
1387 * please submit a patch. */
1388 #if 0
1389 if (SymbolValue(SCAVENGE_READ_ONLY_SPACE) != UNBOUND_MARKER_WIDETAG
1390 && SymbolValue(SCAVENGE_READ_ONLY_SPACE) != NIL) {
1391 unsigned read_only_space_size =
1392 (lispobj *)SymbolValue(READ_ONLY_SPACE_FREE_POINTER) -
1393 (lispobj *)READ_ONLY_SPACE_START;
1394 fprintf(stderr,
1395 "scavenging read only space: %d bytes\n",
1396 read_only_space_size * sizeof(lispobj));
1397 pscav( (lispobj *)READ_ONLY_SPACE_START, read_only_space_size, 0);
1399 #endif
1401 #ifdef PRINTNOISE
1402 printf(" static");
1403 fflush(stdout);
1404 #endif
1405 clean = (lispobj *)STATIC_SPACE_START;
1406 do {
1407 while (clean != static_free)
1408 clean = pscav(clean, static_free - clean, 0);
1409 laters = later_blocks;
1410 count = later_count;
1411 later_blocks = NULL;
1412 later_count = 0;
1413 while (laters != NULL) {
1414 for (i = 0; i < count; i++) {
1415 if (laters->u[i].count == 0) {
1417 } else if (laters->u[i].count <= LATERMAXCOUNT) {
1418 pscav(laters->u[i+1].ptr, laters->u[i].count, 1);
1419 i++;
1420 } else {
1421 pscav(laters->u[i].ptr, 1, 1);
1424 next = laters->next;
1425 free(laters);
1426 laters = next;
1427 count = LATERBLOCKSIZE;
1429 } while (clean != static_free || later_blocks != NULL);
1431 #ifdef PRINTNOISE
1432 printf(" cleanup");
1433 fflush(stdout);
1434 #endif
1436 os_zero((os_vm_address_t) current_dynamic_space,
1437 (os_vm_size_t) DYNAMIC_SPACE_SIZE);
1439 /* Zero the stack. Note that the stack is also zeroed by SUB-GC
1440 * calling SCRUB-CONTROL-STACK - this zeros the stack on the x86. */
1441 #ifndef __i386__
1442 os_zero((os_vm_address_t) current_control_stack_pointer,
1443 (os_vm_size_t) (CONTROL_STACK_SIZE -
1444 ((current_control_stack_pointer -
1445 (lispobj *)CONTROL_STACK_START) *
1446 sizeof(lispobj))));
1447 #endif
1449 /* It helps to update the heap free pointers so that free_heap can
1450 * verify after it's done. */
1451 SetSymbolValue(READ_ONLY_SPACE_FREE_POINTER, (lispobj)read_only_free);
1452 SetSymbolValue(STATIC_SPACE_FREE_POINTER, (lispobj)static_free);
1454 #if !defined(__i386__)
1455 dynamic_space_free_pointer = current_dynamic_space;
1456 #else
1457 #if defined LISP_FEATURE_GENCGC
1458 gc_free_heap();
1459 #else
1460 #error unsupported case /* in CMU CL, was "ibmrt using GC" */
1461 #endif
1462 #endif
1464 #ifdef PRINTNOISE
1465 printf(" done]\n");
1466 fflush(stdout);
1467 #endif
1469 return 0;