2 * C-level stuff to implement Lisp-level PURIFY
6 * This software is part of the SBCL system. See the README file for
9 * This software is derived from the CMU CL system, which was
10 * written at Carnegie Mellon University and released into the
11 * public domain. The software is in the public domain and is
12 * provided with absolutely no warranty. See the COPYING and CREDITS
13 * files for more information.
17 #include <sys/types.h>
25 #include "interrupt.h"
34 #if defined(ibmrt) || defined(__i386__)
35 static lispobj *dynamic_space_free_pointer;
39 lose("GC invariant lost, file \"%s\", line %d", __FILE__, __LINE__)
42 #define gc_assert(ex) do { \
43 if (!(ex)) gc_abort(); \
50 /* These hold the original end of the read_only and static spaces so
51 * we can tell what are forwarding pointers. */
53 static lispobj *read_only_end, *static_end;
55 static lispobj *read_only_free, *static_free;
57 static lispobj *pscav(lispobj *addr, int nwords, boolean constant);
59 #define LATERBLOCKSIZE 1020
60 #define LATERMAXCOUNT 10
68 } *later_blocks = NULL;
69 static int later_count = 0;
71 #define CEILING(x,y) (((x) + ((y) - 1)) & (~((y) - 1)))
72 #define NWORDS(x,y) (CEILING((x),(y)) / (y))
75 #define RAW_ADDR_OFFSET 0
77 #define RAW_ADDR_OFFSET (6*sizeof(lispobj) - type_FunctionPointer)
81 forwarding_pointer_p(lispobj obj)
87 return ((static_end <= ptr && ptr <= static_free) ||
88 (read_only_end <= ptr && ptr <= read_only_free));
92 dynamic_pointer_p(lispobj ptr)
95 /* KLUDGE: This has an implicit dependence on the ordering of
96 * address spaces, and is therefore basically wrong. I'd fix it,
97 * but I don't have a non-386 port to test it on. Porters are
98 * encouraged to fix it. -- WHN 2000-10-17 */
99 return (ptr >= (lispobj)DYNAMIC_SPACE_START);
101 /* Be more conservative, and remember, this is a maybe. */
102 return (ptr >= (lispobj)DYNAMIC_SPACE_START
104 ptr < (lispobj)dynamic_space_free_pointer);
112 /* original x86/CGC stack scavenging code by Paul Werkowski */
115 maybe_can_move_p(lispobj thing)
117 lispobj *thingp,header;
118 if (dynamic_pointer_p(thing)) { /* in dynamic space */
119 thingp = (lispobj*)PTR(thing);
121 if(Pointerp(header) && forwarding_pointer_p(header))
122 return -1; /* must change it */
123 if(LowtagOf(thing) == type_ListPointer)
124 return type_ListPointer; /* can we check this somehow */
125 else if (thing & 3) { /* not fixnum */
126 int kind = TypeOf(header);
127 /* printf(" %x %x",header,kind); */
128 switch (kind) { /* something with a header */
130 case type_SingleFloat:
131 case type_DoubleFloat:
132 #ifdef type_LongFloat
136 case type_SimpleVector:
137 case type_SimpleString:
138 case type_SimpleBitVector:
139 case type_SimpleArrayUnsignedByte2:
140 case type_SimpleArrayUnsignedByte4:
141 case type_SimpleArrayUnsignedByte8:
142 case type_SimpleArrayUnsignedByte16:
143 case type_SimpleArrayUnsignedByte32:
144 #ifdef type_SimpleArraySignedByte8
145 case type_SimpleArraySignedByte8:
147 #ifdef type_SimpleArraySignedByte16
148 case type_SimpleArraySignedByte16:
150 #ifdef type_SimpleArraySignedByte30
151 case type_SimpleArraySignedByte30:
153 #ifdef type_SimpleArraySignedByte32
154 case type_SimpleArraySignedByte32:
156 case type_SimpleArraySingleFloat:
157 case type_SimpleArrayDoubleFloat:
158 #ifdef type_SimpleArrayLongFloat
159 case type_SimpleArrayLongFloat:
161 #ifdef type_SimpleArrayComplexSingleFloat
162 case type_SimpleArrayComplexSingleFloat:
164 #ifdef type_SimpleArrayComplexDoubleFloat
165 case type_SimpleArrayComplexDoubleFloat:
167 #ifdef type_SimpleArrayComplexLongFloat
168 case type_SimpleArrayComplexLongFloat:
170 case type_CodeHeader:
171 case type_FunctionHeader:
172 case type_ClosureFunctionHeader:
173 case type_ReturnPcHeader:
174 case type_ClosureHeader:
175 case type_FuncallableInstanceHeader:
176 case type_InstanceHeader:
177 case type_ValueCellHeader:
178 case type_ByteCodeFunction:
179 case type_ByteCodeClosure:
180 case type_WeakPointer:
190 static int pverbose=0;
191 #define PVERBOSE pverbose
193 carefully_pscav_stack(lispobj*lowaddr, lispobj*base)
195 lispobj*sp = lowaddr;
199 if((unsigned)thing & 0x3) /* may be pointer */
201 /* need to check for valid float/double? */
202 k = maybe_can_move_p(thing);
203 if(PVERBOSE)printf("%8x %8x %d\n",sp, thing, k);
214 * Enhanced x86/GENCGC stack scavenging by Douglas Crosher.
216 * Scavenging the stack on the i386 is problematic due to conservative
217 * roots and raw return addresses. Here it is handled in two passes:
218 * the first pass runs before any objects are moved and tries to
219 * identify valid pointers and return address on the stack, the second
220 * pass scavenges these.
223 static unsigned pointer_filter_verbose = 0;
226 valid_dynamic_space_pointer(lispobj *pointer, lispobj *start_addr)
228 /* If it's not a return address then it needs to be a valid Lisp
230 if (!Pointerp((lispobj)pointer))
233 /* Check that the object pointed to is consistent with the pointer
235 switch (LowtagOf((lispobj)pointer)) {
236 case type_FunctionPointer:
237 /* Start_addr should be the enclosing code object, or a closure
239 switch (TypeOf(*start_addr)) {
240 case type_CodeHeader:
241 /* This case is probably caught above. */
243 case type_ClosureHeader:
244 case type_FuncallableInstanceHeader:
245 case type_ByteCodeFunction:
246 case type_ByteCodeClosure:
247 if ((int)pointer != ((int)start_addr+type_FunctionPointer)) {
248 if (pointer_filter_verbose) {
249 fprintf(stderr,"*Wf2: %x %x %x\n", (unsigned int) pointer,
250 (unsigned int) start_addr, *start_addr);
256 if (pointer_filter_verbose) {
257 fprintf(stderr,"*Wf3: %x %x %x\n", (unsigned int) pointer,
258 (unsigned int) start_addr, *start_addr);
263 case type_ListPointer:
264 if ((int)pointer != ((int)start_addr+type_ListPointer)) {
265 if (pointer_filter_verbose)
266 fprintf(stderr,"*Wl1: %x %x %x\n", (unsigned int) pointer,
267 (unsigned int) start_addr, *start_addr);
270 /* Is it plausible cons? */
271 if((Pointerp(start_addr[0])
272 || ((start_addr[0] & 3) == 0) /* fixnum */
273 || (TypeOf(start_addr[0]) == type_BaseChar)
274 || (TypeOf(start_addr[0]) == type_UnboundMarker))
275 && (Pointerp(start_addr[1])
276 || ((start_addr[1] & 3) == 0) /* fixnum */
277 || (TypeOf(start_addr[1]) == type_BaseChar)
278 || (TypeOf(start_addr[1]) == type_UnboundMarker))) {
281 if (pointer_filter_verbose) {
282 fprintf(stderr,"*Wl2: %x %x %x\n", (unsigned int) pointer,
283 (unsigned int) start_addr, *start_addr);
287 case type_InstancePointer:
288 if ((int)pointer != ((int)start_addr+type_InstancePointer)) {
289 if (pointer_filter_verbose) {
290 fprintf(stderr,"*Wi1: %x %x %x\n", (unsigned int) pointer,
291 (unsigned int) start_addr, *start_addr);
295 if (TypeOf(start_addr[0]) != type_InstanceHeader) {
296 if (pointer_filter_verbose) {
297 fprintf(stderr,"*Wi2: %x %x %x\n", (unsigned int) pointer,
298 (unsigned int) start_addr, *start_addr);
303 case type_OtherPointer:
304 if ((int)pointer != ((int)start_addr+type_OtherPointer)) {
305 if (pointer_filter_verbose) {
306 fprintf(stderr,"*Wo1: %x %x %x\n", (unsigned int) pointer,
307 (unsigned int) start_addr, *start_addr);
311 /* Is it plausible? Not a cons. X should check the headers. */
312 if(Pointerp(start_addr[0]) || ((start_addr[0] & 3) == 0)) {
313 if (pointer_filter_verbose) {
314 fprintf(stderr,"*Wo2: %x %x %x\n", (unsigned int) pointer,
315 (unsigned int) start_addr, *start_addr);
319 switch (TypeOf(start_addr[0])) {
320 case type_UnboundMarker:
322 if (pointer_filter_verbose) {
323 fprintf(stderr,"*Wo3: %x %x %x\n", (unsigned int) pointer,
324 (unsigned int) start_addr, *start_addr);
328 /* only pointed to by function pointers? */
329 case type_ClosureHeader:
330 case type_FuncallableInstanceHeader:
331 case type_ByteCodeFunction:
332 case type_ByteCodeClosure:
333 if (pointer_filter_verbose) {
334 fprintf(stderr,"*Wo4: %x %x %x\n", (unsigned int) pointer,
335 (unsigned int) start_addr, *start_addr);
339 case type_InstanceHeader:
340 if (pointer_filter_verbose) {
341 fprintf(stderr,"*Wo5: %x %x %x\n", (unsigned int) pointer,
342 (unsigned int) start_addr, *start_addr);
346 /* the valid other immediate pointer objects */
347 case type_SimpleVector:
350 #ifdef type_ComplexSingleFloat
351 case type_ComplexSingleFloat:
353 #ifdef type_ComplexDoubleFloat
354 case type_ComplexDoubleFloat:
356 #ifdef type_ComplexLongFloat
357 case type_ComplexLongFloat:
359 case type_SimpleArray:
360 case type_ComplexString:
361 case type_ComplexBitVector:
362 case type_ComplexVector:
363 case type_ComplexArray:
364 case type_ValueCellHeader:
365 case type_SymbolHeader:
367 case type_CodeHeader:
369 case type_SingleFloat:
370 case type_DoubleFloat:
371 #ifdef type_LongFloat
374 case type_SimpleString:
375 case type_SimpleBitVector:
376 case type_SimpleArrayUnsignedByte2:
377 case type_SimpleArrayUnsignedByte4:
378 case type_SimpleArrayUnsignedByte8:
379 case type_SimpleArrayUnsignedByte16:
380 case type_SimpleArrayUnsignedByte32:
381 #ifdef type_SimpleArraySignedByte8
382 case type_SimpleArraySignedByte8:
384 #ifdef type_SimpleArraySignedByte16
385 case type_SimpleArraySignedByte16:
387 #ifdef type_SimpleArraySignedByte30
388 case type_SimpleArraySignedByte30:
390 #ifdef type_SimpleArraySignedByte32
391 case type_SimpleArraySignedByte32:
393 case type_SimpleArraySingleFloat:
394 case type_SimpleArrayDoubleFloat:
395 #ifdef type_SimpleArrayLongFloat
396 case type_SimpleArrayLongFloat:
398 #ifdef type_SimpleArrayComplexSingleFloat
399 case type_SimpleArrayComplexSingleFloat:
401 #ifdef type_SimpleArrayComplexDoubleFloat
402 case type_SimpleArrayComplexDoubleFloat:
404 #ifdef type_SimpleArrayComplexLongFloat
405 case type_SimpleArrayComplexLongFloat:
408 case type_WeakPointer:
412 if (pointer_filter_verbose) {
413 fprintf(stderr,"*Wo6: %x %x %x\n", (unsigned int) pointer,
414 (unsigned int) start_addr, *start_addr);
420 if (pointer_filter_verbose) {
421 fprintf(stderr,"*W?: %x %x %x\n", (unsigned int) pointer,
422 (unsigned int) start_addr, *start_addr);
431 #define MAX_STACK_POINTERS 256
432 lispobj *valid_stack_locations[MAX_STACK_POINTERS];
433 unsigned int num_valid_stack_locations;
435 #define MAX_STACK_RETURN_ADDRESSES 128
436 lispobj *valid_stack_ra_locations[MAX_STACK_RETURN_ADDRESSES];
437 lispobj *valid_stack_ra_code_objects[MAX_STACK_RETURN_ADDRESSES];
438 unsigned int num_valid_stack_ra_locations;
440 /* Identify valid stack slots. */
442 setup_i386_stack_scav(lispobj *lowaddr, lispobj *base)
444 lispobj *sp = lowaddr;
445 num_valid_stack_locations = 0;
446 num_valid_stack_ra_locations = 0;
447 for (sp = lowaddr; sp < base; sp++) {
449 /* Find the object start address */
450 lispobj *start_addr = search_dynamic_space((void *)thing);
452 /* We need to allow raw pointers into Code objects for return
453 * addresses. This will also pick up pointers to functions in code
455 if (TypeOf(*start_addr) == type_CodeHeader) {
456 gc_assert(num_valid_stack_ra_locations < MAX_STACK_RETURN_ADDRESSES);
457 valid_stack_ra_locations[num_valid_stack_ra_locations] = sp;
458 valid_stack_ra_code_objects[num_valid_stack_ra_locations++] =
459 (lispobj *)((int)start_addr + type_OtherPointer);
461 if (valid_dynamic_space_pointer((void *)thing, start_addr)) {
462 gc_assert(num_valid_stack_locations < MAX_STACK_POINTERS);
463 valid_stack_locations[num_valid_stack_locations++] = sp;
468 if (pointer_filter_verbose) {
469 fprintf(stderr, "number of valid stack pointers = %d\n",
470 num_valid_stack_locations);
471 fprintf(stderr, "number of stack return addresses = %d\n",
472 num_valid_stack_ra_locations);
477 pscav_i386_stack(void)
481 for (i = 0; i < num_valid_stack_locations; i++)
482 pscav(valid_stack_locations[i], 1, 0);
484 for (i = 0; i < num_valid_stack_ra_locations; i++) {
485 lispobj code_obj = (lispobj)valid_stack_ra_code_objects[i];
486 pscav(&code_obj, 1, 0);
487 if (pointer_filter_verbose) {
488 fprintf(stderr,"*C moved RA %x to %x; for code object %x to %x\n",
489 *valid_stack_ra_locations[i],
490 (int)(*valid_stack_ra_locations[i])
491 - ((int)valid_stack_ra_code_objects[i] - (int)code_obj),
492 (unsigned int) valid_stack_ra_code_objects[i], code_obj);
494 *valid_stack_ra_locations[i] =
495 ((int)(*valid_stack_ra_locations[i])
496 - ((int)valid_stack_ra_code_objects[i] - (int)code_obj));
504 pscav_later(lispobj *where, int count)
508 if (count > LATERMAXCOUNT) {
509 while (count > LATERMAXCOUNT) {
510 pscav_later(where, LATERMAXCOUNT);
511 count -= LATERMAXCOUNT;
512 where += LATERMAXCOUNT;
516 if (later_blocks == NULL || later_count == LATERBLOCKSIZE ||
517 (later_count == LATERBLOCKSIZE-1 && count > 1)) {
518 new = (struct later *)malloc(sizeof(struct later));
519 new->next = later_blocks;
520 if (later_blocks && later_count < LATERBLOCKSIZE)
521 later_blocks->u[later_count].ptr = NULL;
527 later_blocks->u[later_count++].count = count;
528 later_blocks->u[later_count++].ptr = where;
532 static lispobj ptrans_boxed(lispobj thing, lispobj header, boolean constant)
535 lispobj result, *new, *old;
537 nwords = 1 + HeaderValue(header);
540 old = (lispobj *)PTR(thing);
542 new = read_only_free;
543 read_only_free += CEILING(nwords, 2);
547 static_free += CEILING(nwords, 2);
551 bcopy(old, new, nwords * sizeof(lispobj));
553 /* Deposit forwarding pointer. */
554 result = (lispobj)new | LowtagOf(thing);
558 pscav(new, nwords, constant);
563 /* We need to look at the layout to see whether it is a pure structure
564 * class, and only then can we transport as constant. If it is pure, we can
565 * ALWAYS transport as a constant. */
566 static lispobj ptrans_instance(lispobj thing, lispobj header, boolean constant)
568 lispobj layout = ((struct instance *)PTR(thing))->slots[0];
569 lispobj pure = ((struct instance *)PTR(layout))->slots[15];
573 return (ptrans_boxed(thing, header, 1));
575 return (ptrans_boxed(thing, header, 0));
578 /* Substructure: special case for the COMPACT-INFO-ENVs, where
579 * the instance may have a point to the dynamic space placed
580 * into it (e.g. the cache-name slot), but the lists and arrays
581 * at the time of a purify can be moved to the RO space. */
583 lispobj result, *new, *old;
585 nwords = 1 + HeaderValue(header);
588 old = (lispobj *)PTR(thing);
590 static_free += CEILING(nwords, 2);
593 bcopy(old, new, nwords * sizeof(lispobj));
595 /* Deposit forwarding pointer. */
596 result = (lispobj)new | LowtagOf(thing);
600 pscav(new, nwords, 1);
606 return NIL; /* dummy value: return something ... */
610 static lispobj ptrans_fdefn(lispobj thing, lispobj header)
613 lispobj result, *new, *old, oldfn;
616 nwords = 1 + HeaderValue(header);
619 old = (lispobj *)PTR(thing);
621 static_free += CEILING(nwords, 2);
624 bcopy(old, new, nwords * sizeof(lispobj));
626 /* Deposit forwarding pointer. */
627 result = (lispobj)new | LowtagOf(thing);
630 /* Scavenge the function. */
631 fdefn = (struct fdefn *)new;
632 oldfn = fdefn->function;
633 pscav(&fdefn->function, 1, 0);
634 if ((char *)oldfn + RAW_ADDR_OFFSET == fdefn->raw_addr)
635 fdefn->raw_addr = (char *)fdefn->function + RAW_ADDR_OFFSET;
640 static lispobj ptrans_unboxed(lispobj thing, lispobj header)
643 lispobj result, *new, *old;
645 nwords = 1 + HeaderValue(header);
648 old = (lispobj *)PTR(thing);
649 new = read_only_free;
650 read_only_free += CEILING(nwords, 2);
653 bcopy(old, new, nwords * sizeof(lispobj));
655 /* Deposit forwarding pointer. */
656 result = (lispobj)new | LowtagOf(thing);
662 static lispobj ptrans_vector(lispobj thing, int bits, int extra,
663 boolean boxed, boolean constant)
665 struct vector *vector;
667 lispobj result, *new;
669 vector = (struct vector *)PTR(thing);
670 nwords = 2 + (CEILING((fixnum_value(vector->length)+extra)*bits,32)>>5);
672 if (boxed && !constant) {
674 static_free += CEILING(nwords, 2);
677 new = read_only_free;
678 read_only_free += CEILING(nwords, 2);
681 bcopy(vector, new, nwords * sizeof(lispobj));
683 result = (lispobj)new | LowtagOf(thing);
684 vector->header = result;
687 pscav(new, nwords, constant);
694 apply_code_fixups_during_purify(struct code *old_code, struct code *new_code)
696 int nheader_words, ncode_words, nwords;
697 void *constants_start_addr, *constants_end_addr;
698 void *code_start_addr, *code_end_addr;
699 lispobj fixups = NIL;
700 unsigned displacement = (unsigned)new_code - (unsigned)old_code;
701 struct vector *fixups_vector;
703 /* Byte compiled code has no fixups. The trace table offset will be
704 * a fixnum if it's x86 compiled code - check. */
705 if (new_code->trace_table_offset & 0x3)
708 /* Else it's x86 machine code. */
709 ncode_words = fixnum_value(new_code->code_size);
710 nheader_words = HeaderValue(*(lispobj *)new_code);
711 nwords = ncode_words + nheader_words;
713 constants_start_addr = (void *)new_code + 5*4;
714 constants_end_addr = (void *)new_code + nheader_words*4;
715 code_start_addr = (void *)new_code + nheader_words*4;
716 code_end_addr = (void *)new_code + nwords*4;
718 /* The first constant should be a pointer to the fixups for this
719 * code objects. Check. */
720 fixups = new_code->constants[0];
722 /* It will be 0 or the unbound-marker if there are no fixups, and
723 * will be an other-pointer to a vector if it is valid. */
724 if ((fixups==0) || (fixups==type_UnboundMarker) || !Pointerp(fixups)) {
726 /* Check for a possible errors. */
727 sniff_code_object(new_code,displacement);
732 fixups_vector = (struct vector *)PTR(fixups);
734 /* Could be pointing to a forwarding pointer. */
735 if (Pointerp(fixups) && (dynamic_pointer_p(fixups))
736 && forwarding_pointer_p(*(lispobj *)fixups_vector)) {
737 /* If so then follow it. */
738 fixups_vector = (struct vector *)PTR(*(lispobj *)fixups_vector);
741 if (TypeOf(fixups_vector->header) == type_SimpleArrayUnsignedByte32) {
742 /* We got the fixups for the code block. Now work through the vector,
743 * and apply a fixup at each address. */
744 int length = fixnum_value(fixups_vector->length);
746 for (i=0; i<length; i++) {
747 unsigned offset = fixups_vector->data[i];
748 /* Now check the current value of offset. */
749 unsigned old_value = *(unsigned *)((unsigned)code_start_addr + offset);
751 /* If it's within the old_code object then it must be an
752 * absolute fixup (relative ones are not saved) */
753 if ((old_value>=(unsigned)old_code)
754 && (old_value<((unsigned)old_code + nwords*4)))
755 /* So add the dispacement. */
756 *(unsigned *)((unsigned)code_start_addr + offset) = old_value
759 /* It is outside the old code object so it must be a relative
760 * fixup (absolute fixups are not saved). So subtract the
762 *(unsigned *)((unsigned)code_start_addr + offset) = old_value
767 /* No longer need the fixups. */
768 new_code->constants[0] = 0;
771 /* Check for possible errors. */
772 sniff_code_object(new_code,displacement);
777 static lispobj ptrans_code(lispobj thing)
779 struct code *code, *new;
781 lispobj func, result;
783 code = (struct code *)PTR(thing);
784 nwords = HeaderValue(code->header) + fixnum_value(code->code_size);
786 new = (struct code *)read_only_free;
787 read_only_free += CEILING(nwords, 2);
789 bcopy(code, new, nwords * sizeof(lispobj));
792 apply_code_fixups_during_purify(code,new);
795 result = (lispobj)new | type_OtherPointer;
797 /* Stick in a forwarding pointer for the code object. */
798 *(lispobj *)code = result;
800 /* Put in forwarding pointers for all the functions. */
801 for (func = code->entry_points;
803 func = ((struct function *)PTR(func))->next) {
805 gc_assert(LowtagOf(func) == type_FunctionPointer);
807 *(lispobj *)PTR(func) = result + (func - thing);
810 /* Arrange to scavenge the debug info later. */
811 pscav_later(&new->debug_info, 1);
813 if(new->trace_table_offset & 0x3)
815 pscav(&new->trace_table_offset, 1, 0);
817 new->trace_table_offset = NIL; /* limit lifetime */
820 /* Scavenge the constants. */
821 pscav(new->constants, HeaderValue(new->header)-5, 1);
823 /* Scavenge all the functions. */
824 pscav(&new->entry_points, 1, 1);
825 for (func = new->entry_points;
827 func = ((struct function *)PTR(func))->next) {
828 gc_assert(LowtagOf(func) == type_FunctionPointer);
829 gc_assert(!dynamic_pointer_p(func));
832 /* Temporarly convert the self pointer to a real function
834 ((struct function *)PTR(func))->self -= RAW_ADDR_OFFSET;
836 pscav(&((struct function *)PTR(func))->self, 2, 1);
838 ((struct function *)PTR(func))->self += RAW_ADDR_OFFSET;
840 pscav_later(&((struct function *)PTR(func))->name, 3);
846 static lispobj ptrans_func(lispobj thing, lispobj header)
849 lispobj code, *new, *old, result;
850 struct function *function;
852 /* Thing can either be a function header, a closure function
853 * header, a closure, or a funcallable-instance. If it's a closure
854 * or a funcallable-instance, we do the same as ptrans_boxed.
855 * Otherwise we have to do something strange, 'cause it is buried
856 * inside a code object. */
858 if (TypeOf(header) == type_FunctionHeader ||
859 TypeOf(header) == type_ClosureFunctionHeader) {
861 /* We can only end up here if the code object has not been
862 * scavenged, because if it had been scavenged, forwarding pointers
863 * would have been left behind for all the entry points. */
865 function = (struct function *)PTR(thing);
866 code = (PTR(thing)-(HeaderValue(function->header)*sizeof(lispobj))) |
869 /* This will cause the function's header to be replaced with a
870 * forwarding pointer. */
873 /* So we can just return that. */
874 return function->header;
877 /* It's some kind of closure-like thing. */
878 nwords = 1 + HeaderValue(header);
879 old = (lispobj *)PTR(thing);
881 /* Allocate the new one. */
882 if (TypeOf(header) == type_FuncallableInstanceHeader) {
883 /* FINs *must* not go in read_only space. */
885 static_free += CEILING(nwords, 2);
888 /* Closures can always go in read-only space, 'cause they
891 new = read_only_free;
892 read_only_free += CEILING(nwords, 2);
895 bcopy(old, new, nwords * sizeof(lispobj));
897 /* Deposit forwarding pointer. */
898 result = (lispobj)new | LowtagOf(thing);
902 pscav(new, nwords, 0);
908 static lispobj ptrans_returnpc(lispobj thing, lispobj header)
912 /* Find the corresponding code object. */
913 code = thing - HeaderValue(header)*sizeof(lispobj);
915 /* Make sure it's been transported. */
916 new = *(lispobj *)PTR(code);
917 if (!forwarding_pointer_p(new))
918 new = ptrans_code(code);
920 /* Maintain the offset: */
921 return new + (thing - code);
924 #define WORDS_PER_CONS CEILING(sizeof(struct cons) / sizeof(lispobj), 2)
926 static lispobj ptrans_list(lispobj thing, boolean constant)
928 struct cons *old, *new, *orig;
932 orig = (struct cons *)read_only_free;
934 orig = (struct cons *)static_free;
938 /* Allocate a new cons cell. */
939 old = (struct cons *)PTR(thing);
941 new = (struct cons *)read_only_free;
942 read_only_free += WORDS_PER_CONS;
945 new = (struct cons *)static_free;
946 static_free += WORDS_PER_CONS;
949 /* Copy the cons cell and keep a pointer to the cdr. */
951 thing = new->cdr = old->cdr;
953 /* Set up the forwarding pointer. */
954 *(lispobj *)old = ((lispobj)new) | type_ListPointer;
956 /* And count this cell. */
958 } while (LowtagOf(thing) == type_ListPointer &&
959 dynamic_pointer_p(thing) &&
960 !(forwarding_pointer_p(*(lispobj *)PTR(thing))));
962 /* Scavenge the list we just copied. */
963 pscav((lispobj *)orig, length * WORDS_PER_CONS, constant);
965 return ((lispobj)orig) | type_ListPointer;
968 static lispobj ptrans_otherptr(lispobj thing, lispobj header, boolean constant)
970 switch (TypeOf(header)) {
972 case type_SingleFloat:
973 case type_DoubleFloat:
974 #ifdef type_LongFloat
977 #ifdef type_ComplexSingleFloat
978 case type_ComplexSingleFloat:
980 #ifdef type_ComplexDoubleFloat
981 case type_ComplexDoubleFloat:
983 #ifdef type_ComplexLongFloat
984 case type_ComplexLongFloat:
987 return ptrans_unboxed(thing, header);
991 case type_SimpleArray:
992 case type_ComplexString:
993 case type_ComplexVector:
994 case type_ComplexArray:
995 return ptrans_boxed(thing, header, constant);
997 case type_ValueCellHeader:
998 case type_WeakPointer:
999 return ptrans_boxed(thing, header, 0);
1001 case type_SymbolHeader:
1002 return ptrans_boxed(thing, header, 0);
1004 case type_SimpleString:
1005 return ptrans_vector(thing, 8, 1, 0, constant);
1007 case type_SimpleBitVector:
1008 return ptrans_vector(thing, 1, 0, 0, constant);
1010 case type_SimpleVector:
1011 return ptrans_vector(thing, 32, 0, 1, constant);
1013 case type_SimpleArrayUnsignedByte2:
1014 return ptrans_vector(thing, 2, 0, 0, constant);
1016 case type_SimpleArrayUnsignedByte4:
1017 return ptrans_vector(thing, 4, 0, 0, constant);
1019 case type_SimpleArrayUnsignedByte8:
1020 #ifdef type_SimpleArraySignedByte8
1021 case type_SimpleArraySignedByte8:
1023 return ptrans_vector(thing, 8, 0, 0, constant);
1025 case type_SimpleArrayUnsignedByte16:
1026 #ifdef type_SimpleArraySignedByte16
1027 case type_SimpleArraySignedByte16:
1029 return ptrans_vector(thing, 16, 0, 0, constant);
1031 case type_SimpleArrayUnsignedByte32:
1032 #ifdef type_SimpleArraySignedByte30
1033 case type_SimpleArraySignedByte30:
1035 #ifdef type_SimpleArraySignedByte32
1036 case type_SimpleArraySignedByte32:
1038 return ptrans_vector(thing, 32, 0, 0, constant);
1040 case type_SimpleArraySingleFloat:
1041 return ptrans_vector(thing, 32, 0, 0, constant);
1043 case type_SimpleArrayDoubleFloat:
1044 return ptrans_vector(thing, 64, 0, 0, constant);
1046 #ifdef type_SimpleArrayLongFloat
1047 case type_SimpleArrayLongFloat:
1049 return ptrans_vector(thing, 96, 0, 0, constant);
1052 return ptrans_vector(thing, 128, 0, 0, constant);
1056 #ifdef type_SimpleArrayComplexSingleFloat
1057 case type_SimpleArrayComplexSingleFloat:
1058 return ptrans_vector(thing, 64, 0, 0, constant);
1061 #ifdef type_SimpleArrayComplexDoubleFloat
1062 case type_SimpleArrayComplexDoubleFloat:
1063 return ptrans_vector(thing, 128, 0, 0, constant);
1066 #ifdef type_SimpleArrayComplexLongFloat
1067 case type_SimpleArrayComplexLongFloat:
1069 return ptrans_vector(thing, 192, 0, 0, constant);
1072 return ptrans_vector(thing, 256, 0, 0, constant);
1076 case type_CodeHeader:
1077 return ptrans_code(thing);
1079 case type_ReturnPcHeader:
1080 return ptrans_returnpc(thing, header);
1083 return ptrans_fdefn(thing, header);
1086 /* Should only come across other pointers to the above stuff. */
1092 static int pscav_fdefn(struct fdefn *fdefn)
1096 fix_func = ((char *)(fdefn->function+RAW_ADDR_OFFSET) == fdefn->raw_addr);
1097 pscav(&fdefn->name, 1, 1);
1098 pscav(&fdefn->function, 1, 0);
1100 fdefn->raw_addr = (char *)(fdefn->function + RAW_ADDR_OFFSET);
1101 return sizeof(struct fdefn) / sizeof(lispobj);
1105 /* now putting code objects in static space */
1107 pscav_code(struct code*code)
1111 nwords = HeaderValue(code->header) + fixnum_value(code->code_size);
1113 /* pw--The trace_table_offset slot can contain a list pointer. This
1114 * occurs when the code object is a top level form that initializes
1115 * a byte-compiled function. The fact that PURIFY was ignoring this
1116 * slot may be a bug unrelated to the x86 port, except that TLF's
1117 * normally become unreachable after the loader calls them and
1118 * won't be seen by PURIFY at all!! */
1119 if(code->trace_table_offset & 0x3)
1121 pscav(&code->trace_table_offset, 1, 0);
1123 code->trace_table_offset = NIL; /* limit lifetime */
1126 /* Arrange to scavenge the debug info later. */
1127 pscav_later(&code->debug_info, 1);
1129 /* Scavenge the constants. */
1130 pscav(code->constants, HeaderValue(code->header)-5, 1);
1132 /* Scavenge all the functions. */
1133 pscav(&code->entry_points, 1, 1);
1134 for (func = code->entry_points;
1136 func = ((struct function *)PTR(func))->next) {
1137 gc_assert(LowtagOf(func) == type_FunctionPointer);
1138 gc_assert(!dynamic_pointer_p(func));
1141 /* Temporarly convert the self pointer to a real function
1143 ((struct function *)PTR(func))->self -= RAW_ADDR_OFFSET;
1145 pscav(&((struct function *)PTR(func))->self, 2, 1);
1147 ((struct function *)PTR(func))->self += RAW_ADDR_OFFSET;
1149 pscav_later(&((struct function *)PTR(func))->name, 3);
1152 return CEILING(nwords,2);
1156 static lispobj *pscav(lispobj *addr, int nwords, boolean constant)
1158 lispobj thing, *thingp, header;
1159 int count = 0; /* (0 = dummy init value to stop GCC warning) */
1160 struct vector *vector;
1162 while (nwords > 0) {
1164 if (Pointerp(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 *)PTR(thing);
1170 if (Pointerp(header) && forwarding_pointer_p(header))
1171 /* Yep, so just copy the forwarding pointer. */
1174 /* Nope, copy the object. */
1175 switch (LowtagOf(thing)) {
1176 case type_FunctionPointer:
1177 thing = ptrans_func(thing, header);
1180 case type_ListPointer:
1181 thing = ptrans_list(thing, constant);
1184 case type_InstancePointer:
1185 thing = ptrans_instance(thing, header, constant);
1188 case type_OtherPointer:
1189 thing = ptrans_otherptr(thing, header, constant);
1193 /* It was a pointer, but not one of them? */
1201 else if (thing & 3) {
1202 /* It's an other immediate. Maybe the header for an unboxed */
1204 switch (TypeOf(thing)) {
1206 case type_SingleFloat:
1207 case type_DoubleFloat:
1208 #ifdef type_LongFloat
1209 case type_LongFloat:
1212 /* It's an unboxed simple object. */
1213 count = HeaderValue(thing)+1;
1216 case type_SimpleVector:
1217 if (HeaderValue(thing) == subtype_VectorValidHashing)
1218 *addr = (subtype_VectorMustRehash<<type_Bits) |
1223 case type_SimpleString:
1224 vector = (struct vector *)addr;
1225 count = CEILING(NWORDS(fixnum_value(vector->length)+1,4)+2,2);
1228 case type_SimpleBitVector:
1229 vector = (struct vector *)addr;
1230 count = CEILING(NWORDS(fixnum_value(vector->length),32)+2,2);
1233 case type_SimpleArrayUnsignedByte2:
1234 vector = (struct vector *)addr;
1235 count = CEILING(NWORDS(fixnum_value(vector->length),16)+2,2);
1238 case type_SimpleArrayUnsignedByte4:
1239 vector = (struct vector *)addr;
1240 count = CEILING(NWORDS(fixnum_value(vector->length),8)+2,2);
1243 case type_SimpleArrayUnsignedByte8:
1244 #ifdef type_SimpleArraySignedByte8
1245 case type_SimpleArraySignedByte8:
1247 vector = (struct vector *)addr;
1248 count = CEILING(NWORDS(fixnum_value(vector->length),4)+2,2);
1251 case type_SimpleArrayUnsignedByte16:
1252 #ifdef type_SimpleArraySignedByte16
1253 case type_SimpleArraySignedByte16:
1255 vector = (struct vector *)addr;
1256 count = CEILING(NWORDS(fixnum_value(vector->length),2)+2,2);
1259 case type_SimpleArrayUnsignedByte32:
1260 #ifdef type_SimpleArraySignedByte30
1261 case type_SimpleArraySignedByte30:
1263 #ifdef type_SimpleArraySignedByte32
1264 case type_SimpleArraySignedByte32:
1266 vector = (struct vector *)addr;
1267 count = CEILING(fixnum_value(vector->length)+2,2);
1270 case type_SimpleArraySingleFloat:
1271 vector = (struct vector *)addr;
1272 count = CEILING(fixnum_value(vector->length)+2,2);
1275 case type_SimpleArrayDoubleFloat:
1276 #ifdef type_SimpleArrayComplexSingleFloat
1277 case type_SimpleArrayComplexSingleFloat:
1279 vector = (struct vector *)addr;
1280 count = fixnum_value(vector->length)*2+2;
1283 #ifdef type_SimpleArrayLongFloat
1284 case type_SimpleArrayLongFloat:
1285 vector = (struct vector *)addr;
1287 count = fixnum_value(vector->length)*3+2;
1290 count = fixnum_value(vector->length)*4+2;
1295 #ifdef type_SimpleArrayComplexDoubleFloat
1296 case type_SimpleArrayComplexDoubleFloat:
1297 vector = (struct vector *)addr;
1298 count = fixnum_value(vector->length)*4+2;
1302 #ifdef type_SimpleArrayComplexLongFloat
1303 case type_SimpleArrayComplexLongFloat:
1304 vector = (struct vector *)addr;
1306 count = fixnum_value(vector->length)*6+2;
1309 count = fixnum_value(vector->length)*8+2;
1314 case type_CodeHeader:
1316 gc_abort(); /* no code headers in static space */
1318 count = pscav_code((struct code*)addr);
1322 case type_FunctionHeader:
1323 case type_ClosureFunctionHeader:
1324 case type_ReturnPcHeader:
1325 /* We should never hit any of these, 'cause they occur
1326 * buried in the middle of code objects. */
1331 case type_ClosureHeader:
1332 case type_FuncallableInstanceHeader:
1333 case type_ByteCodeFunction:
1334 case type_ByteCodeClosure:
1335 /* The function self pointer needs special care on the
1336 * x86 because it is the real entry point. */
1338 lispobj fun = ((struct closure *)addr)->function
1340 pscav(&fun, 1, constant);
1341 ((struct closure *)addr)->function = fun + RAW_ADDR_OFFSET;
1347 case type_WeakPointer:
1348 /* Weak pointers get preserved during purify, 'cause I
1349 * don't feel like figuring out how to break them. */
1350 pscav(addr+1, 2, constant);
1355 /* We have to handle fdefn objects specially, so we
1356 * can fix up the raw function address. */
1357 count = pscav_fdefn((struct fdefn *)addr);
1366 /* It's a fixnum. */
1377 int purify(lispobj static_roots, lispobj read_only_roots)
1381 struct later *laters, *next;
1384 printf("[doing purification:");
1388 if (fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX)) != 0) {
1389 /* FIXME: 1. What does this mean? 2. It shouldn't be reporting
1390 * its error simply by a. printing a string b. to stdout instead
1392 printf(" Ack! Can't purify interrupt contexts. ");
1397 #if defined(ibmrt) || defined(__i386__)
1398 dynamic_space_free_pointer =
1399 (lispobj*)SymbolValue(ALLOCATION_POINTER);
1402 read_only_end = read_only_free =
1403 (lispobj *)SymbolValue(READ_ONLY_SPACE_FREE_POINTER);
1404 static_end = static_free =
1405 (lispobj *)SymbolValue(STATIC_SPACE_FREE_POINTER);
1413 gc_assert((lispobj *)CONTROL_STACK_END > ((&read_only_roots)+1));
1414 setup_i386_stack_scav(((&static_roots)-2), (lispobj *)CONTROL_STACK_END);
1417 pscav(&static_roots, 1, 0);
1418 pscav(&read_only_roots, 1, 1);
1421 printf(" handlers");
1424 pscav((lispobj *) interrupt_handlers,
1425 sizeof(interrupt_handlers) / sizeof(lispobj),
1433 pscav((lispobj *)control_stack,
1434 current_control_stack_pointer - (lispobj *)CONTROL_STACK_START,
1441 gc_assert((lispobj *)control_stack_end > ((&read_only_roots)+1));
1442 carefully_pscav_stack(((&read_only_roots)+1),
1443 (lispobj *)CONTROL_STACK_END);
1448 printf(" bindings");
1451 #if !defined(ibmrt) && !defined(__i386__)
1452 pscav( (lispobj *)BINDING_STACK_START,
1453 (lispobj *)current_binding_stack_pointer - (lispobj *)BINDING_STACK_START,
1456 pscav( (lispobj *)BINDING_STACK_START,
1457 (lispobj *)SymbolValue(BINDING_STACK_POINTER) -
1458 (lispobj *)BINDING_STACK_START,
1462 #ifdef SCAVENGE_READ_ONLY_SPACE
1463 if (SymbolValue(SCAVENGE_READ_ONLY_SPACE) != type_UnboundMarker
1464 && SymbolValue(SCAVENGE_READ_ONLY_SPACE) != NIL) {
1465 unsigned read_only_space_size =
1466 (lispobj *)SymbolValue(READ_ONLY_SPACE_FREE_POINTER) -
1467 (lispobj *)READ_ONLY_SPACE_START;
1469 "scavenging read only space: %d bytes\n",
1470 read_only_space_size * sizeof(lispobj));
1471 pscav( (lispobj *)READ_ONLY_SPACE_START, read_only_space_size, 0);
1479 clean = (lispobj *)STATIC_SPACE_START;
1481 while (clean != static_free)
1482 clean = pscav(clean, static_free - clean, 0);
1483 laters = later_blocks;
1484 count = later_count;
1485 later_blocks = NULL;
1487 while (laters != NULL) {
1488 for (i = 0; i < count; i++) {
1489 if (laters->u[i].count == 0) {
1491 } else if (laters->u[i].count <= LATERMAXCOUNT) {
1492 pscav(laters->u[i+1].ptr, laters->u[i].count, 1);
1495 pscav(laters->u[i].ptr, 1, 1);
1498 next = laters->next;
1501 count = LATERBLOCKSIZE;
1503 } while (clean != static_free || later_blocks != NULL);
1510 #if defined(WANT_CGC) && defined(X86_CGC_ACTIVE_P)
1511 if(SymbolValue(X86_CGC_ACTIVE_P) != T) {
1512 os_zero((os_vm_address_t) DYNAMIC_SPACE_START,
1513 (os_vm_size_t) DYNAMIC_SPACE_SIZE);
1516 os_zero((os_vm_address_t) DYNAMIC_SPACE_START,
1517 (os_vm_size_t) DYNAMIC_SPACE_SIZE);
1520 /* Zero the stack. Note that the stack is also zeroed by SUB-GC
1521 * calling SCRUB-CONTROL-STACK - this zeros the stack on the x86. */
1523 os_zero((os_vm_address_t) current_control_stack_pointer,
1524 (os_vm_size_t) (CONTROL_STACK_SIZE -
1525 ((current_control_stack_pointer -
1526 (lispobj *)CONTROL_STACK_START) *
1530 #if defined(WANT_CGC) && defined(STATIC_BLUE_BAG)
1532 lispobj bag = SymbolValue(STATIC_BLUE_BAG);
1533 struct cons*cons = (struct cons*)static_free;
1534 struct cons*pair = cons + 1;
1535 static_free += 2*WORDS_PER_CONS;
1536 if(bag == type_UnboundMarker)
1539 cons->car = (lispobj)pair | type_ListPointer;
1540 pair->car = (lispobj)static_end;
1541 pair->cdr = (lispobj)static_free;
1542 bag = (lispobj)cons | type_ListPointer;
1543 SetSymbolValue(STATIC_BLUE_BAG, bag);
1547 /* It helps to update the heap free pointers so that free_heap can
1548 * verify after it's done. */
1549 SetSymbolValue(READ_ONLY_SPACE_FREE_POINTER, (lispobj)read_only_free);
1550 SetSymbolValue(STATIC_SPACE_FREE_POINTER, (lispobj)static_free);
1552 #if !defined(ibmrt) && !defined(__i386__)
1553 dynamic_space_free_pointer = DYNAMIC_SPACE_START;
1555 #if defined(WANT_CGC) && defined(X86_CGC_ACTIVE_P)
1557 if(SymbolValue(X86_CGC_ACTIVE_P) != T)
1558 SetSymbolValue(ALLOCATION_POINTER, (lispobj)DYNAMIC_SPACE_START);
1565 /* ibmrt using GC */
1566 SetSymbolValue(ALLOCATION_POINTER, (lispobj)DYNAMIC_SPACE_START);