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.
21 #include <sys/types.h>
29 #include "interrupt.h"
38 #if defined(ibmrt) || defined(__i386__)
39 static lispobj *current_dynamic_space_free_pointer;
43 lose("GC invariant lost, file \"%s\", line %d", __FILE__, __LINE__)
46 #define gc_assert(ex) do { \
47 if (!(ex)) gc_abort(); \
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
72 } *later_blocks = NULL;
73 static int later_count = 0;
75 #define CEILING(x,y) (((x) + ((y) - 1)) & (~((y) - 1)))
76 #define NWORDS(x,y) (CEILING((x),(y)) / (y))
79 #define RAW_ADDR_OFFSET 0
81 #define RAW_ADDR_OFFSET (6*sizeof(lispobj) - type_FunctionPointer)
85 forwarding_pointer_p(lispobj obj)
91 return ((static_end <= ptr && ptr <= static_free) ||
92 (read_only_end <= ptr && ptr <= read_only_free));
96 dynamic_pointer_p(lispobj ptr)
99 return (ptr >= (lispobj)dynamic_0_space);
101 /* Be more conservative, and remember, this is a maybe. */
102 return (ptr >= (lispobj)current_dynamic_space
104 ptr < (lispobj)current_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", pointer, start_addr, *start_addr);
255 if (pointer_filter_verbose) {
256 fprintf(stderr,"*Wf3: %x %x %x\n", pointer, start_addr, *start_addr);
261 case type_ListPointer:
262 if ((int)pointer != ((int)start_addr+type_ListPointer)) {
263 if (pointer_filter_verbose)
264 fprintf(stderr,"*Wl1: %x %x %x\n", pointer, start_addr, *start_addr);
267 /* Is it plausible cons? */
268 if((Pointerp(start_addr[0])
269 || ((start_addr[0] & 3) == 0) /* fixnum */
270 || (TypeOf(start_addr[0]) == type_BaseChar)
271 || (TypeOf(start_addr[0]) == type_UnboundMarker))
272 && (Pointerp(start_addr[1])
273 || ((start_addr[1] & 3) == 0) /* fixnum */
274 || (TypeOf(start_addr[1]) == type_BaseChar)
275 || (TypeOf(start_addr[1]) == type_UnboundMarker))) {
278 if (pointer_filter_verbose) {
279 fprintf(stderr,"*Wl2: %x %x %x\n", pointer, start_addr, *start_addr);
283 case type_InstancePointer:
284 if ((int)pointer != ((int)start_addr+type_InstancePointer)) {
285 if (pointer_filter_verbose) {
286 fprintf(stderr,"*Wi1: %x %x %x\n", pointer, start_addr, *start_addr);
290 if (TypeOf(start_addr[0]) != type_InstanceHeader) {
291 if (pointer_filter_verbose) {
292 fprintf(stderr,"*Wi2: %x %x %x\n", pointer, start_addr, *start_addr);
297 case type_OtherPointer:
298 if ((int)pointer != ((int)start_addr+type_OtherPointer)) {
299 if (pointer_filter_verbose) {
300 fprintf(stderr,"*Wo1: %x %x %x\n", pointer, start_addr, *start_addr);
304 /* Is it plausible? Not a cons. X should check the headers. */
305 if(Pointerp(start_addr[0]) || ((start_addr[0] & 3) == 0)) {
306 if (pointer_filter_verbose) {
307 fprintf(stderr,"*Wo2: %x %x %x\n", pointer, start_addr, *start_addr);
311 switch (TypeOf(start_addr[0])) {
312 case type_UnboundMarker:
314 if (pointer_filter_verbose) {
315 fprintf(stderr,"*Wo3: %x %x %x\n", pointer, start_addr, *start_addr);
319 /* only pointed to by function pointers? */
320 case type_ClosureHeader:
321 case type_FuncallableInstanceHeader:
322 case type_ByteCodeFunction:
323 case type_ByteCodeClosure:
324 if (pointer_filter_verbose) {
325 fprintf(stderr,"*Wo4: %x %x %x\n", pointer, start_addr, *start_addr);
329 case type_InstanceHeader:
330 if (pointer_filter_verbose) {
331 fprintf(stderr,"*Wo5: %x %x %x\n", pointer, start_addr, *start_addr);
335 /* the valid other immediate pointer objects */
336 case type_SimpleVector:
339 #ifdef type_ComplexSingleFloat
340 case type_ComplexSingleFloat:
342 #ifdef type_ComplexDoubleFloat
343 case type_ComplexDoubleFloat:
345 #ifdef type_ComplexLongFloat
346 case type_ComplexLongFloat:
348 case type_SimpleArray:
349 case type_ComplexString:
350 case type_ComplexBitVector:
351 case type_ComplexVector:
352 case type_ComplexArray:
353 case type_ValueCellHeader:
354 case type_SymbolHeader:
356 case type_CodeHeader:
358 case type_SingleFloat:
359 case type_DoubleFloat:
360 #ifdef type_LongFloat
363 case type_SimpleString:
364 case type_SimpleBitVector:
365 case type_SimpleArrayUnsignedByte2:
366 case type_SimpleArrayUnsignedByte4:
367 case type_SimpleArrayUnsignedByte8:
368 case type_SimpleArrayUnsignedByte16:
369 case type_SimpleArrayUnsignedByte32:
370 #ifdef type_SimpleArraySignedByte8
371 case type_SimpleArraySignedByte8:
373 #ifdef type_SimpleArraySignedByte16
374 case type_SimpleArraySignedByte16:
376 #ifdef type_SimpleArraySignedByte30
377 case type_SimpleArraySignedByte30:
379 #ifdef type_SimpleArraySignedByte32
380 case type_SimpleArraySignedByte32:
382 case type_SimpleArraySingleFloat:
383 case type_SimpleArrayDoubleFloat:
384 #ifdef type_SimpleArrayLongFloat
385 case type_SimpleArrayLongFloat:
387 #ifdef type_SimpleArrayComplexSingleFloat
388 case type_SimpleArrayComplexSingleFloat:
390 #ifdef type_SimpleArrayComplexDoubleFloat
391 case type_SimpleArrayComplexDoubleFloat:
393 #ifdef type_SimpleArrayComplexLongFloat
394 case type_SimpleArrayComplexLongFloat:
397 case type_WeakPointer:
401 if (pointer_filter_verbose) {
402 fprintf(stderr,"*Wo6: %x %x %x\n", pointer, start_addr, *start_addr);
408 if (pointer_filter_verbose) {
409 fprintf(stderr,"*W?: %x %x %x\n", pointer, start_addr, *start_addr);
418 #define MAX_STACK_POINTERS 256
419 lispobj *valid_stack_locations[MAX_STACK_POINTERS];
420 unsigned int num_valid_stack_locations;
422 #define MAX_STACK_RETURN_ADDRESSES 128
423 lispobj *valid_stack_ra_locations[MAX_STACK_RETURN_ADDRESSES];
424 lispobj *valid_stack_ra_code_objects[MAX_STACK_RETURN_ADDRESSES];
425 unsigned int num_valid_stack_ra_locations;
427 /* Identify valid stack slots. */
429 setup_i386_stack_scav(lispobj *lowaddr, lispobj *base)
431 lispobj *sp = lowaddr;
432 num_valid_stack_locations = 0;
433 num_valid_stack_ra_locations = 0;
434 for (sp = lowaddr; sp < base; sp++) {
436 /* Find the object start address */
437 lispobj *start_addr = search_dynamic_space((void *)thing);
439 /* We need to allow raw pointers into Code objects for return
440 * addresses. This will also pick up pointers to functions in code
442 if (TypeOf(*start_addr) == type_CodeHeader) {
443 gc_assert(num_valid_stack_ra_locations < MAX_STACK_RETURN_ADDRESSES);
444 valid_stack_ra_locations[num_valid_stack_ra_locations] = sp;
445 valid_stack_ra_code_objects[num_valid_stack_ra_locations++] =
446 (lispobj *)((int)start_addr + type_OtherPointer);
448 if (valid_dynamic_space_pointer((void *)thing, start_addr)) {
449 gc_assert(num_valid_stack_locations < MAX_STACK_POINTERS);
450 valid_stack_locations[num_valid_stack_locations++] = sp;
455 if (pointer_filter_verbose) {
456 fprintf(stderr, "number of valid stack pointers = %d\n",
457 num_valid_stack_locations);
458 fprintf(stderr, "number of stack return addresses = %d\n",
459 num_valid_stack_ra_locations);
464 pscav_i386_stack(void)
468 for (i = 0; i < num_valid_stack_locations; i++)
469 pscav(valid_stack_locations[i], 1, 0);
471 for (i = 0; i < num_valid_stack_ra_locations; i++) {
472 lispobj code_obj = (lispobj)valid_stack_ra_code_objects[i];
473 pscav(&code_obj, 1, 0);
474 if (pointer_filter_verbose) {
475 fprintf(stderr,"*C moved RA %x to %x; for code object %x to %x\n",
476 *valid_stack_ra_locations[i],
477 (int)(*valid_stack_ra_locations[i])
478 - ((int)valid_stack_ra_code_objects[i] - (int)code_obj),
479 valid_stack_ra_code_objects[i], code_obj);
481 *valid_stack_ra_locations[i] =
482 ((int)(*valid_stack_ra_locations[i])
483 - ((int)valid_stack_ra_code_objects[i] - (int)code_obj));
491 pscav_later(lispobj *where, int 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;
519 static lispobj ptrans_boxed(lispobj thing, lispobj header, boolean constant)
522 lispobj result, *new, *old;
524 nwords = 1 + HeaderValue(header);
527 old = (lispobj *)PTR(thing);
529 new = read_only_free;
530 read_only_free += CEILING(nwords, 2);
534 static_free += CEILING(nwords, 2);
538 bcopy(old, new, nwords * sizeof(lispobj));
540 /* Deposit forwarding pointer. */
541 result = (lispobj)new | LowtagOf(thing);
545 pscav(new, nwords, constant);
550 /* We need to look at the layout to see whether it is a pure structure
551 * class, and only then can we transport as constant. If it is pure, we can
552 * ALWAYS transport as a constant. */
553 static lispobj ptrans_instance(lispobj thing, lispobj header, boolean constant)
555 lispobj layout = ((struct instance *)PTR(thing))->slots[0];
556 lispobj pure = ((struct instance *)PTR(layout))->slots[15];
560 return (ptrans_boxed(thing, header, 1));
562 return (ptrans_boxed(thing, header, 0));
565 /* Substructure: special case for the compact-info-envs, where
566 * the instance may have a point to the dynamic space placed
567 * into it (e.g. the cache-name slot), but the lists and arrays
568 * at the time of a purify can be moved to the RO space. */
570 lispobj result, *new, *old;
572 nwords = 1 + HeaderValue(header);
575 old = (lispobj *)PTR(thing);
577 static_free += CEILING(nwords, 2);
580 bcopy(old, new, nwords * sizeof(lispobj));
582 /* Deposit forwarding pointer. */
583 result = (lispobj)new | LowtagOf(thing);
587 pscav(new, nwords, 1);
596 static lispobj ptrans_fdefn(lispobj thing, lispobj header)
599 lispobj result, *new, *old, oldfn;
602 nwords = 1 + HeaderValue(header);
605 old = (lispobj *)PTR(thing);
607 static_free += CEILING(nwords, 2);
610 bcopy(old, new, nwords * sizeof(lispobj));
612 /* Deposit forwarding pointer. */
613 result = (lispobj)new | LowtagOf(thing);
616 /* Scavenge the function. */
617 fdefn = (struct fdefn *)new;
618 oldfn = fdefn->function;
619 pscav(&fdefn->function, 1, 0);
620 if ((char *)oldfn + RAW_ADDR_OFFSET == fdefn->raw_addr)
621 fdefn->raw_addr = (char *)fdefn->function + RAW_ADDR_OFFSET;
626 static lispobj ptrans_unboxed(lispobj thing, lispobj header)
629 lispobj result, *new, *old;
631 nwords = 1 + HeaderValue(header);
634 old = (lispobj *)PTR(thing);
635 new = read_only_free;
636 read_only_free += CEILING(nwords, 2);
639 bcopy(old, new, nwords * sizeof(lispobj));
641 /* Deposit forwarding pointer. */
642 result = (lispobj)new | LowtagOf(thing);
648 static lispobj ptrans_vector(lispobj thing, int bits, int extra,
649 boolean boxed, boolean constant)
651 struct vector *vector;
653 lispobj result, *new;
655 vector = (struct vector *)PTR(thing);
656 nwords = 2 + (CEILING((fixnum_value(vector->length)+extra)*bits,32)>>5);
658 if (boxed && !constant) {
660 static_free += CEILING(nwords, 2);
663 new = read_only_free;
664 read_only_free += CEILING(nwords, 2);
667 bcopy(vector, new, nwords * sizeof(lispobj));
669 result = (lispobj)new | LowtagOf(thing);
670 vector->header = result;
673 pscav(new, nwords, constant);
680 apply_code_fixups_during_purify(struct code *old_code, struct code *new_code)
682 int nheader_words, ncode_words, nwords;
683 void *constants_start_addr, *constants_end_addr;
684 void *code_start_addr, *code_end_addr;
686 lispobj fixups = NIL;
687 unsigned displacement = (unsigned)new_code - (unsigned)old_code;
688 struct vector *fixups_vector;
690 /* Byte compiled code has no fixups. The trace table offset will be
691 * a fixnum if it's x86 compiled code - check. */
692 if (new_code->trace_table_offset & 0x3)
695 /* Else it's x86 machine code. */
696 ncode_words = fixnum_value(new_code->code_size);
697 nheader_words = HeaderValue(*(lispobj *)new_code);
698 nwords = ncode_words + nheader_words;
700 constants_start_addr = (void *)new_code + 5*4;
701 constants_end_addr = (void *)new_code + nheader_words*4;
702 code_start_addr = (void *)new_code + nheader_words*4;
703 code_end_addr = (void *)new_code + nwords*4;
705 /* The first constant should be a pointer to the fixups for this
706 * code objects. Check. */
707 fixups = new_code->constants[0];
709 /* It will be 0 or the unbound-marker if there are no fixups, and
710 * will be an other-pointer to a vector if it is valid. */
711 if ((fixups==0) || (fixups==type_UnboundMarker) || !Pointerp(fixups)) {
713 /* Check for a possible errors. */
714 sniff_code_object(new_code,displacement);
719 fixups_vector = (struct vector *)PTR(fixups);
721 /* Could be pointing to a forwarding pointer. */
722 if (Pointerp(fixups) && (dynamic_pointer_p(fixups))
723 && forwarding_pointer_p(*(lispobj *)fixups_vector)) {
724 /* If so then follow it. */
725 fixups_vector = (struct vector *)PTR(*(lispobj *)fixups_vector);
728 if (TypeOf(fixups_vector->header) == type_SimpleArrayUnsignedByte32) {
729 /* We got the fixups for the code block. Now work through the vector,
730 * and apply a fixup at each address. */
731 int length = fixnum_value(fixups_vector->length);
733 for (i=0; i<length; i++) {
734 unsigned offset = fixups_vector->data[i];
735 /* Now check the current value of offset. */
736 unsigned old_value = *(unsigned *)((unsigned)code_start_addr + offset);
738 /* If it's within the old_code object then it must be an
739 * absolute fixup (relative ones are not saved) */
740 if ((old_value>=(unsigned)old_code)
741 && (old_value<((unsigned)old_code + nwords*4)))
742 /* So add the dispacement. */
743 *(unsigned *)((unsigned)code_start_addr + offset) = old_value
746 /* It is outside the old code object so it must be a relative
747 * fixup (absolute fixups are not saved). So subtract the
749 *(unsigned *)((unsigned)code_start_addr + offset) = old_value
754 /* No longer need the fixups. */
755 new_code->constants[0] = 0;
758 /* Check for possible errors. */
759 sniff_code_object(new_code,displacement);
764 static lispobj ptrans_code(lispobj thing)
766 struct code *code, *new;
768 lispobj func, result;
770 code = (struct code *)PTR(thing);
771 nwords = HeaderValue(code->header) + fixnum_value(code->code_size);
773 new = (struct code *)read_only_free;
774 read_only_free += CEILING(nwords, 2);
776 bcopy(code, new, nwords * sizeof(lispobj));
779 apply_code_fixups_during_purify(code,new);
782 result = (lispobj)new | type_OtherPointer;
784 /* Stick in a forwarding pointer for the code object. */
785 *(lispobj *)code = result;
787 /* Put in forwarding pointers for all the functions. */
788 for (func = code->entry_points;
790 func = ((struct function *)PTR(func))->next) {
792 gc_assert(LowtagOf(func) == type_FunctionPointer);
794 *(lispobj *)PTR(func) = result + (func - thing);
797 /* Arrange to scavenge the debug info later. */
798 pscav_later(&new->debug_info, 1);
800 if(new->trace_table_offset & 0x3)
802 pscav(&new->trace_table_offset, 1, 0);
804 new->trace_table_offset = NIL; /* limit lifetime */
807 /* Scavenge the constants. */
808 pscav(new->constants, HeaderValue(new->header)-5, 1);
810 /* Scavenge all the functions. */
811 pscav(&new->entry_points, 1, 1);
812 for (func = new->entry_points;
814 func = ((struct function *)PTR(func))->next) {
815 gc_assert(LowtagOf(func) == type_FunctionPointer);
816 gc_assert(!dynamic_pointer_p(func));
819 /* Temporarly convert the self pointer to a real function
821 ((struct function *)PTR(func))->self -= RAW_ADDR_OFFSET;
823 pscav(&((struct function *)PTR(func))->self, 2, 1);
825 ((struct function *)PTR(func))->self += RAW_ADDR_OFFSET;
827 pscav_later(&((struct function *)PTR(func))->name, 3);
833 static lispobj ptrans_func(lispobj thing, lispobj header)
836 lispobj code, *new, *old, result;
837 struct function *function;
839 /* Thing can either be a function header, a closure function
840 * header, a closure, or a funcallable-instance. If it's a closure
841 * or a funcallable-instance, we do the same as ptrans_boxed.
842 * Otherwise we have to do something strange, 'cause it is buried
843 * inside a code object. */
845 if (TypeOf(header) == type_FunctionHeader ||
846 TypeOf(header) == type_ClosureFunctionHeader) {
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 function *)PTR(thing);
853 code = (PTR(thing)-(HeaderValue(function->header)*sizeof(lispobj))) |
856 /* This will cause the function's header to be replaced with a
857 * forwarding pointer. */
860 /* So we can just return that. */
861 return function->header;
864 /* It's some kind of closure-like thing. */
865 nwords = 1 + HeaderValue(header);
866 old = (lispobj *)PTR(thing);
868 /* Allocate the new one. */
869 if (TypeOf(header) == type_FuncallableInstanceHeader) {
870 /* FINs *must* not go in read_only space. */
872 static_free += CEILING(nwords, 2);
875 /* Closures can always go in read-only space, 'cause they
878 new = read_only_free;
879 read_only_free += CEILING(nwords, 2);
882 bcopy(old, new, nwords * sizeof(lispobj));
884 /* Deposit forwarding pointer. */
885 result = (lispobj)new | LowtagOf(thing);
889 pscav(new, nwords, 0);
895 static lispobj ptrans_returnpc(lispobj thing, lispobj header)
899 /* Find the corresponding code object. */
900 code = thing - HeaderValue(header)*sizeof(lispobj);
902 /* Make sure it's been transported. */
903 new = *(lispobj *)PTR(code);
904 if (!forwarding_pointer_p(new))
905 new = ptrans_code(code);
907 /* Maintain the offset: */
908 return new + (thing - code);
911 #define WORDS_PER_CONS CEILING(sizeof(struct cons) / sizeof(lispobj), 2)
913 static lispobj ptrans_list(lispobj thing, boolean constant)
915 struct cons *old, *new, *orig;
919 orig = (struct cons *)read_only_free;
921 orig = (struct cons *)static_free;
925 /* Allocate a new cons cell. */
926 old = (struct cons *)PTR(thing);
928 new = (struct cons *)read_only_free;
929 read_only_free += WORDS_PER_CONS;
932 new = (struct cons *)static_free;
933 static_free += WORDS_PER_CONS;
936 /* Copy the cons cell and keep a pointer to the cdr. */
938 thing = new->cdr = old->cdr;
940 /* Set up the forwarding pointer. */
941 *(lispobj *)old = ((lispobj)new) | type_ListPointer;
943 /* And count this cell. */
945 } while (LowtagOf(thing) == type_ListPointer &&
946 dynamic_pointer_p(thing) &&
947 !(forwarding_pointer_p(*(lispobj *)PTR(thing))));
949 /* Scavenge the list we just copied. */
950 pscav((lispobj *)orig, length * WORDS_PER_CONS, constant);
952 return ((lispobj)orig) | type_ListPointer;
955 static lispobj ptrans_otherptr(lispobj thing, lispobj header, boolean constant)
957 switch (TypeOf(header)) {
959 case type_SingleFloat:
960 case type_DoubleFloat:
961 #ifdef type_LongFloat
964 #ifdef type_ComplexSingleFloat
965 case type_ComplexSingleFloat:
967 #ifdef type_ComplexDoubleFloat
968 case type_ComplexDoubleFloat:
970 #ifdef type_ComplexLongFloat
971 case type_ComplexLongFloat:
974 return ptrans_unboxed(thing, header);
978 case type_SimpleArray:
979 case type_ComplexString:
980 case type_ComplexVector:
981 case type_ComplexArray:
982 return ptrans_boxed(thing, header, constant);
984 case type_ValueCellHeader:
985 case type_WeakPointer:
986 return ptrans_boxed(thing, header, 0);
988 case type_SymbolHeader:
989 return ptrans_boxed(thing, header, 0);
991 case type_SimpleString:
992 return ptrans_vector(thing, 8, 1, 0, constant);
994 case type_SimpleBitVector:
995 return ptrans_vector(thing, 1, 0, 0, constant);
997 case type_SimpleVector:
998 return ptrans_vector(thing, 32, 0, 1, constant);
1000 case type_SimpleArrayUnsignedByte2:
1001 return ptrans_vector(thing, 2, 0, 0, constant);
1003 case type_SimpleArrayUnsignedByte4:
1004 return ptrans_vector(thing, 4, 0, 0, constant);
1006 case type_SimpleArrayUnsignedByte8:
1007 #ifdef type_SimpleArraySignedByte8
1008 case type_SimpleArraySignedByte8:
1010 return ptrans_vector(thing, 8, 0, 0, constant);
1012 case type_SimpleArrayUnsignedByte16:
1013 #ifdef type_SimpleArraySignedByte16
1014 case type_SimpleArraySignedByte16:
1016 return ptrans_vector(thing, 16, 0, 0, constant);
1018 case type_SimpleArrayUnsignedByte32:
1019 #ifdef type_SimpleArraySignedByte30
1020 case type_SimpleArraySignedByte30:
1022 #ifdef type_SimpleArraySignedByte32
1023 case type_SimpleArraySignedByte32:
1025 return ptrans_vector(thing, 32, 0, 0, constant);
1027 case type_SimpleArraySingleFloat:
1028 return ptrans_vector(thing, 32, 0, 0, constant);
1030 case type_SimpleArrayDoubleFloat:
1031 return ptrans_vector(thing, 64, 0, 0, constant);
1033 #ifdef type_SimpleArrayLongFloat
1034 case type_SimpleArrayLongFloat:
1036 return ptrans_vector(thing, 96, 0, 0, constant);
1039 return ptrans_vector(thing, 128, 0, 0, constant);
1043 #ifdef type_SimpleArrayComplexSingleFloat
1044 case type_SimpleArrayComplexSingleFloat:
1045 return ptrans_vector(thing, 64, 0, 0, constant);
1048 #ifdef type_SimpleArrayComplexDoubleFloat
1049 case type_SimpleArrayComplexDoubleFloat:
1050 return ptrans_vector(thing, 128, 0, 0, constant);
1053 #ifdef type_SimpleArrayComplexLongFloat
1054 case type_SimpleArrayComplexLongFloat:
1056 return ptrans_vector(thing, 192, 0, 0, constant);
1059 return ptrans_vector(thing, 256, 0, 0, constant);
1063 case type_CodeHeader:
1064 return ptrans_code(thing);
1066 case type_ReturnPcHeader:
1067 return ptrans_returnpc(thing, header);
1070 return ptrans_fdefn(thing, header);
1073 /* Should only come across other pointers to the above stuff. */
1079 static int pscav_fdefn(struct fdefn *fdefn)
1083 fix_func = ((char *)(fdefn->function+RAW_ADDR_OFFSET) == fdefn->raw_addr);
1084 pscav(&fdefn->name, 1, 1);
1085 pscav(&fdefn->function, 1, 0);
1087 fdefn->raw_addr = (char *)(fdefn->function + RAW_ADDR_OFFSET);
1088 return sizeof(struct fdefn) / sizeof(lispobj);
1092 /* now putting code objects in static space */
1094 pscav_code(struct code*code)
1098 nwords = HeaderValue(code->header) + fixnum_value(code->code_size);
1100 /* pw--The trace_table_offset slot can contain a list pointer. This
1101 * occurs when the code object is a top level form that initializes
1102 * a byte-compiled function. The fact that PURIFY was ignoring this
1103 * slot may be a bug unrelated to the x86 port, except that TLF's
1104 * normally become unreachable after the loader calls them and
1105 * won't be seen by PURIFY at all!! */
1106 if(code->trace_table_offset & 0x3)
1108 pscav(&code->trace_table_offset, 1, 0);
1110 code->trace_table_offset = NIL; /* limit lifetime */
1113 /* Arrange to scavenge the debug info later. */
1114 pscav_later(&code->debug_info, 1);
1116 /* Scavenge the constants. */
1117 pscav(code->constants, HeaderValue(code->header)-5, 1);
1119 /* Scavenge all the functions. */
1120 pscav(&code->entry_points, 1, 1);
1121 for (func = code->entry_points;
1123 func = ((struct function *)PTR(func))->next) {
1124 gc_assert(LowtagOf(func) == type_FunctionPointer);
1125 gc_assert(!dynamic_pointer_p(func));
1128 /* Temporarly convert the self pointer to a real function
1130 ((struct function *)PTR(func))->self -= RAW_ADDR_OFFSET;
1132 pscav(&((struct function *)PTR(func))->self, 2, 1);
1134 ((struct function *)PTR(func))->self += RAW_ADDR_OFFSET;
1136 pscav_later(&((struct function *)PTR(func))->name, 3);
1139 return CEILING(nwords,2);
1143 static lispobj *pscav(lispobj *addr, int nwords, boolean constant)
1145 lispobj thing, *thingp, header;
1147 struct vector *vector;
1149 while (nwords > 0) {
1151 if (Pointerp(thing)) {
1152 /* It's a pointer. Is it something we might have to move? */
1153 if (dynamic_pointer_p(thing)) {
1154 /* Maybe. Have we already moved it? */
1155 thingp = (lispobj *)PTR(thing);
1157 if (Pointerp(header) && forwarding_pointer_p(header))
1158 /* Yep, so just copy the forwarding pointer. */
1161 /* Nope, copy the object. */
1162 switch (LowtagOf(thing)) {
1163 case type_FunctionPointer:
1164 thing = ptrans_func(thing, header);
1167 case type_ListPointer:
1168 thing = ptrans_list(thing, constant);
1171 case type_InstancePointer:
1172 thing = ptrans_instance(thing, header, constant);
1175 case type_OtherPointer:
1176 thing = ptrans_otherptr(thing, header, constant);
1180 /* It was a pointer, but not one of them? */
1188 else if (thing & 3) {
1189 /* It's an other immediate. Maybe the header for an unboxed */
1191 switch (TypeOf(thing)) {
1193 case type_SingleFloat:
1194 case type_DoubleFloat:
1195 #ifdef type_LongFloat
1196 case type_LongFloat:
1199 /* It's an unboxed simple object. */
1200 count = HeaderValue(thing)+1;
1203 case type_SimpleVector:
1204 if (HeaderValue(thing) == subtype_VectorValidHashing)
1205 *addr = (subtype_VectorMustRehash<<type_Bits) |
1210 case type_SimpleString:
1211 vector = (struct vector *)addr;
1212 count = CEILING(NWORDS(fixnum_value(vector->length)+1,4)+2,2);
1215 case type_SimpleBitVector:
1216 vector = (struct vector *)addr;
1217 count = CEILING(NWORDS(fixnum_value(vector->length),32)+2,2);
1220 case type_SimpleArrayUnsignedByte2:
1221 vector = (struct vector *)addr;
1222 count = CEILING(NWORDS(fixnum_value(vector->length),16)+2,2);
1225 case type_SimpleArrayUnsignedByte4:
1226 vector = (struct vector *)addr;
1227 count = CEILING(NWORDS(fixnum_value(vector->length),8)+2,2);
1230 case type_SimpleArrayUnsignedByte8:
1231 #ifdef type_SimpleArraySignedByte8
1232 case type_SimpleArraySignedByte8:
1234 vector = (struct vector *)addr;
1235 count = CEILING(NWORDS(fixnum_value(vector->length),4)+2,2);
1238 case type_SimpleArrayUnsignedByte16:
1239 #ifdef type_SimpleArraySignedByte16
1240 case type_SimpleArraySignedByte16:
1242 vector = (struct vector *)addr;
1243 count = CEILING(NWORDS(fixnum_value(vector->length),2)+2,2);
1246 case type_SimpleArrayUnsignedByte32:
1247 #ifdef type_SimpleArraySignedByte30
1248 case type_SimpleArraySignedByte30:
1250 #ifdef type_SimpleArraySignedByte32
1251 case type_SimpleArraySignedByte32:
1253 vector = (struct vector *)addr;
1254 count = CEILING(fixnum_value(vector->length)+2,2);
1257 case type_SimpleArraySingleFloat:
1258 vector = (struct vector *)addr;
1259 count = CEILING(fixnum_value(vector->length)+2,2);
1262 case type_SimpleArrayDoubleFloat:
1263 #ifdef type_SimpleArrayComplexSingleFloat
1264 case type_SimpleArrayComplexSingleFloat:
1266 vector = (struct vector *)addr;
1267 count = fixnum_value(vector->length)*2+2;
1270 #ifdef type_SimpleArrayLongFloat
1271 case type_SimpleArrayLongFloat:
1272 vector = (struct vector *)addr;
1274 count = fixnum_value(vector->length)*3+2;
1277 count = fixnum_value(vector->length)*4+2;
1282 #ifdef type_SimpleArrayComplexDoubleFloat
1283 case type_SimpleArrayComplexDoubleFloat:
1284 vector = (struct vector *)addr;
1285 count = fixnum_value(vector->length)*4+2;
1289 #ifdef type_SimpleArrayComplexLongFloat
1290 case type_SimpleArrayComplexLongFloat:
1291 vector = (struct vector *)addr;
1293 count = fixnum_value(vector->length)*6+2;
1296 count = fixnum_value(vector->length)*8+2;
1301 case type_CodeHeader:
1303 gc_abort(); /* no code headers in static space */
1305 count = pscav_code((struct code*)addr);
1309 case type_FunctionHeader:
1310 case type_ClosureFunctionHeader:
1311 case type_ReturnPcHeader:
1312 /* We should never hit any of these, 'cause they occur
1313 * buried in the middle of code objects. */
1318 case type_ClosureHeader:
1319 case type_FuncallableInstanceHeader:
1320 case type_ByteCodeFunction:
1321 case type_ByteCodeClosure:
1322 /* The function self pointer needs special care on the
1323 * x86 because it is the real entry point. */
1325 lispobj fun = ((struct closure *)addr)->function
1327 pscav(&fun, 1, constant);
1328 ((struct closure *)addr)->function = fun + RAW_ADDR_OFFSET;
1334 case type_WeakPointer:
1335 /* Weak pointers get preserved during purify, 'cause I
1336 * don't feel like figuring out how to break them. */
1337 pscav(addr+1, 2, constant);
1342 /* We have to handle fdefn objects specially, so we
1343 * can fix up the raw function address. */
1344 count = pscav_fdefn((struct fdefn *)addr);
1353 /* It's a fixnum. */
1364 int purify(lispobj static_roots, lispobj read_only_roots)
1368 struct later *laters, *next;
1371 printf("[doing purification:");
1375 if (fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX)) != 0) {
1376 /* FIXME: 1. What does this mean? 2. It shouldn't be reporting
1377 * its error simply by a. printing a string b. to stdout instead
1379 printf(" Ack! Can't purify interrupt contexts. ");
1384 #if defined(ibmrt) || defined(__i386__)
1385 current_dynamic_space_free_pointer =
1386 (lispobj*)SymbolValue(ALLOCATION_POINTER);
1389 read_only_end = read_only_free =
1390 (lispobj *)SymbolValue(READ_ONLY_SPACE_FREE_POINTER);
1391 static_end = static_free =
1392 (lispobj *)SymbolValue(STATIC_SPACE_FREE_POINTER);
1400 gc_assert(control_stack_end > ((&read_only_roots)+1));
1401 setup_i386_stack_scav(((&static_roots)-2), control_stack_end);
1404 pscav(&static_roots, 1, 0);
1405 pscav(&read_only_roots, 1, 1);
1408 printf(" handlers");
1411 pscav((lispobj *) interrupt_handlers,
1412 sizeof(interrupt_handlers) / sizeof(lispobj),
1420 pscav(control_stack, current_control_stack_pointer - control_stack, 0);
1426 gc_assert(control_stack_end > ((&read_only_roots)+1));
1427 carefully_pscav_stack(((&read_only_roots)+1), control_stack_end);
1432 printf(" bindings");
1435 #if !defined(ibmrt) && !defined(__i386__)
1436 pscav(binding_stack, current_binding_stack_pointer - binding_stack, 0);
1438 pscav(binding_stack, (lispobj *)SymbolValue(BINDING_STACK_POINTER) - binding_stack, 0);
1441 #ifdef SCAVENGE_READ_ONLY_SPACE
1442 if (SymbolValue(SCAVENGE_READ_ONLY_SPACE) != type_UnboundMarker
1443 && SymbolValue(SCAVENGE_READ_ONLY_SPACE) != NIL) {
1444 unsigned read_only_space_size =
1445 (lispobj *)SymbolValue(READ_ONLY_SPACE_FREE_POINTER) - read_only_space;
1447 "scavenging read only space: %d bytes\n",
1448 read_only_space_size * sizeof(lispobj));
1449 pscav(read_only_space, read_only_space_size, 0);
1457 clean = static_space;
1459 while (clean != static_free)
1460 clean = pscav(clean, static_free - clean, 0);
1461 laters = later_blocks;
1462 count = later_count;
1463 later_blocks = NULL;
1465 while (laters != NULL) {
1466 for (i = 0; i < count; i++) {
1467 if (laters->u[i].count == 0) {
1469 } else if (laters->u[i].count <= LATERMAXCOUNT) {
1470 pscav(laters->u[i+1].ptr, laters->u[i].count, 1);
1473 pscav(laters->u[i].ptr, 1, 1);
1476 next = laters->next;
1479 count = LATERBLOCKSIZE;
1481 } while (clean != static_free || later_blocks != NULL);
1488 #if defined(WANT_CGC) && defined(X86_CGC_ACTIVE_P)
1489 if(SymbolValue(X86_CGC_ACTIVE_P) != T)
1490 os_zero((os_vm_address_t) current_dynamic_space,
1491 (os_vm_size_t) DYNAMIC_SPACE_SIZE);
1493 os_zero((os_vm_address_t) current_dynamic_space,
1494 (os_vm_size_t) DYNAMIC_SPACE_SIZE);
1497 /* Zero the stack. Note that the stack is also zeroed by SUB-GC
1498 * calling SCRUB-CONTROL-STACK - this zeros the stack on the x86. */
1500 os_zero((os_vm_address_t) current_control_stack_pointer,
1501 (os_vm_size_t) (CONTROL_STACK_SIZE -
1502 ((current_control_stack_pointer - control_stack) *
1506 #if defined(WANT_CGC) && defined(STATIC_BLUE_BAG)
1508 lispobj bag = SymbolValue(STATIC_BLUE_BAG);
1509 struct cons*cons = (struct cons*)static_free;
1510 struct cons*pair = cons + 1;
1511 static_free += 2*WORDS_PER_CONS;
1512 if(bag == type_UnboundMarker)
1515 cons->car = (lispobj)pair | type_ListPointer;
1516 pair->car = (lispobj)static_end;
1517 pair->cdr = (lispobj)static_free;
1518 bag = (lispobj)cons | type_ListPointer;
1519 SetSymbolValue(STATIC_BLUE_BAG, bag);
1523 /* It helps to update the heap free pointers so that free_heap can
1524 * verify after it's done. */
1525 SetSymbolValue(READ_ONLY_SPACE_FREE_POINTER, (lispobj)read_only_free);
1526 SetSymbolValue(STATIC_SPACE_FREE_POINTER, (lispobj)static_free);
1528 #if !defined(ibmrt) && !defined(__i386__)
1529 current_dynamic_space_free_pointer = current_dynamic_space;
1531 #if defined(WANT_CGC) && defined(X86_CGC_ACTIVE_P)
1533 if(SymbolValue(X86_CGC_ACTIVE_P) != T)
1534 SetSymbolValue(ALLOCATION_POINTER, (lispobj)current_dynamic_space);
1541 /* ibmrt using GC */
1542 SetSymbolValue(ALLOCATION_POINTER, (lispobj)current_dynamic_space);