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"
35 /* again, what's so special about the x86 that this is differently
36 * visible there than on other platforms? -dan 20010125
38 static lispobj *dynamic_space_free_pointer;
42 lose("GC invariant lost, file \"%s\", line %d", __FILE__, __LINE__)
45 #define gc_assert(ex) do { \
46 if (!(ex)) gc_abort(); \
53 /* These hold the original end of the read_only and static spaces so
54 * we can tell what are forwarding pointers. */
56 static lispobj *read_only_end, *static_end;
58 static lispobj *read_only_free, *static_free;
60 static lispobj *pscav(lispobj *addr, int nwords, boolean constant);
62 #define LATERBLOCKSIZE 1020
63 #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 /* KLUDGE: This has an implicit dependence on the ordering of
100 * address spaces, and is therefore basically wrong. I'd fix it,
101 * but I don't have a non-386 port to test it on. Porters are
102 * encouraged to fix it. -- WHN 2000-10-17 */
103 return (ptr >= (lispobj)DYNAMIC_SPACE_START);
105 /* Be more conservative, and remember, this is a maybe. */
106 return (ptr >= (lispobj)DYNAMIC_SPACE_START
108 ptr < (lispobj)dynamic_space_free_pointer);
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;
129 valid_dynamic_space_pointer(lispobj *pointer, lispobj *start_addr)
131 /* If it's not a return address then it needs to be a valid Lisp
133 if (!Pointerp((lispobj)pointer))
136 /* Check that the object pointed to is consistent with the pointer
138 switch (LowtagOf((lispobj)pointer)) {
139 case type_FunctionPointer:
140 /* Start_addr should be the enclosing code object, or a closure
142 switch (TypeOf(*start_addr)) {
143 case type_CodeHeader:
144 /* This case is probably caught above. */
146 case type_ClosureHeader:
147 case type_FuncallableInstanceHeader:
148 case type_ByteCodeFunction:
149 case type_ByteCodeClosure:
150 if ((int)pointer != ((int)start_addr+type_FunctionPointer)) {
151 if (pointer_filter_verbose) {
152 fprintf(stderr,"*Wf2: %x %x %x\n", (unsigned int) pointer,
153 (unsigned int) start_addr, *start_addr);
159 if (pointer_filter_verbose) {
160 fprintf(stderr,"*Wf3: %x %x %x\n", (unsigned int) pointer,
161 (unsigned int) start_addr, *start_addr);
166 case type_ListPointer:
167 if ((int)pointer != ((int)start_addr+type_ListPointer)) {
168 if (pointer_filter_verbose)
169 fprintf(stderr,"*Wl1: %x %x %x\n", (unsigned int) pointer,
170 (unsigned int) start_addr, *start_addr);
173 /* Is it plausible cons? */
174 if((Pointerp(start_addr[0])
175 || ((start_addr[0] & 3) == 0) /* fixnum */
176 || (TypeOf(start_addr[0]) == type_BaseChar)
177 || (TypeOf(start_addr[0]) == type_UnboundMarker))
178 && (Pointerp(start_addr[1])
179 || ((start_addr[1] & 3) == 0) /* fixnum */
180 || (TypeOf(start_addr[1]) == type_BaseChar)
181 || (TypeOf(start_addr[1]) == type_UnboundMarker))) {
184 if (pointer_filter_verbose) {
185 fprintf(stderr,"*Wl2: %x %x %x\n", (unsigned int) pointer,
186 (unsigned int) start_addr, *start_addr);
190 case type_InstancePointer:
191 if ((int)pointer != ((int)start_addr+type_InstancePointer)) {
192 if (pointer_filter_verbose) {
193 fprintf(stderr,"*Wi1: %x %x %x\n", (unsigned int) pointer,
194 (unsigned int) start_addr, *start_addr);
198 if (TypeOf(start_addr[0]) != type_InstanceHeader) {
199 if (pointer_filter_verbose) {
200 fprintf(stderr,"*Wi2: %x %x %x\n", (unsigned int) pointer,
201 (unsigned int) start_addr, *start_addr);
206 case type_OtherPointer:
207 if ((int)pointer != ((int)start_addr+type_OtherPointer)) {
208 if (pointer_filter_verbose) {
209 fprintf(stderr,"*Wo1: %x %x %x\n", (unsigned int) pointer,
210 (unsigned int) start_addr, *start_addr);
214 /* Is it plausible? Not a cons. X should check the headers. */
215 if(Pointerp(start_addr[0]) || ((start_addr[0] & 3) == 0)) {
216 if (pointer_filter_verbose) {
217 fprintf(stderr,"*Wo2: %x %x %x\n", (unsigned int) pointer,
218 (unsigned int) start_addr, *start_addr);
222 switch (TypeOf(start_addr[0])) {
223 case type_UnboundMarker:
225 if (pointer_filter_verbose) {
226 fprintf(stderr,"*Wo3: %x %x %x\n", (unsigned int) pointer,
227 (unsigned int) start_addr, *start_addr);
231 /* only pointed to by function pointers? */
232 case type_ClosureHeader:
233 case type_FuncallableInstanceHeader:
234 case type_ByteCodeFunction:
235 case type_ByteCodeClosure:
236 if (pointer_filter_verbose) {
237 fprintf(stderr,"*Wo4: %x %x %x\n", (unsigned int) pointer,
238 (unsigned int) start_addr, *start_addr);
242 case type_InstanceHeader:
243 if (pointer_filter_verbose) {
244 fprintf(stderr,"*Wo5: %x %x %x\n", (unsigned int) pointer,
245 (unsigned int) start_addr, *start_addr);
249 /* the valid other immediate pointer objects */
250 case type_SimpleVector:
253 #ifdef type_ComplexSingleFloat
254 case type_ComplexSingleFloat:
256 #ifdef type_ComplexDoubleFloat
257 case type_ComplexDoubleFloat:
259 #ifdef type_ComplexLongFloat
260 case type_ComplexLongFloat:
262 case type_SimpleArray:
263 case type_ComplexString:
264 case type_ComplexBitVector:
265 case type_ComplexVector:
266 case type_ComplexArray:
267 case type_ValueCellHeader:
268 case type_SymbolHeader:
270 case type_CodeHeader:
272 case type_SingleFloat:
273 case type_DoubleFloat:
274 #ifdef type_LongFloat
277 case type_SimpleString:
278 case type_SimpleBitVector:
279 case type_SimpleArrayUnsignedByte2:
280 case type_SimpleArrayUnsignedByte4:
281 case type_SimpleArrayUnsignedByte8:
282 case type_SimpleArrayUnsignedByte16:
283 case type_SimpleArrayUnsignedByte32:
284 #ifdef type_SimpleArraySignedByte8
285 case type_SimpleArraySignedByte8:
287 #ifdef type_SimpleArraySignedByte16
288 case type_SimpleArraySignedByte16:
290 #ifdef type_SimpleArraySignedByte30
291 case type_SimpleArraySignedByte30:
293 #ifdef type_SimpleArraySignedByte32
294 case type_SimpleArraySignedByte32:
296 case type_SimpleArraySingleFloat:
297 case type_SimpleArrayDoubleFloat:
298 #ifdef type_SimpleArrayLongFloat
299 case type_SimpleArrayLongFloat:
301 #ifdef type_SimpleArrayComplexSingleFloat
302 case type_SimpleArrayComplexSingleFloat:
304 #ifdef type_SimpleArrayComplexDoubleFloat
305 case type_SimpleArrayComplexDoubleFloat:
307 #ifdef type_SimpleArrayComplexLongFloat
308 case type_SimpleArrayComplexLongFloat:
311 case type_WeakPointer:
315 if (pointer_filter_verbose) {
316 fprintf(stderr,"*Wo6: %x %x %x\n", (unsigned int) pointer,
317 (unsigned int) start_addr, *start_addr);
323 if (pointer_filter_verbose) {
324 fprintf(stderr,"*W?: %x %x %x\n", (unsigned int) pointer,
325 (unsigned int) start_addr, *start_addr);
334 #define MAX_STACK_POINTERS 256
335 lispobj *valid_stack_locations[MAX_STACK_POINTERS];
336 unsigned int num_valid_stack_locations;
338 #define MAX_STACK_RETURN_ADDRESSES 128
339 lispobj *valid_stack_ra_locations[MAX_STACK_RETURN_ADDRESSES];
340 lispobj *valid_stack_ra_code_objects[MAX_STACK_RETURN_ADDRESSES];
341 unsigned int num_valid_stack_ra_locations;
343 /* Identify valid stack slots. */
345 setup_i386_stack_scav(lispobj *lowaddr, lispobj *base)
347 lispobj *sp = lowaddr;
348 num_valid_stack_locations = 0;
349 num_valid_stack_ra_locations = 0;
350 for (sp = lowaddr; sp < base; sp++) {
352 /* Find the object start address */
353 lispobj *start_addr = search_dynamic_space((void *)thing);
355 /* We need to allow raw pointers into Code objects for
356 * return addresses. This will also pick up pointers to
357 * functions in code objects. */
358 if (TypeOf(*start_addr) == type_CodeHeader) {
359 gc_assert(num_valid_stack_ra_locations <
360 MAX_STACK_RETURN_ADDRESSES);
361 valid_stack_ra_locations[num_valid_stack_ra_locations] = sp;
362 valid_stack_ra_code_objects[num_valid_stack_ra_locations++] =
363 (lispobj *)((int)start_addr + type_OtherPointer);
365 if (valid_dynamic_space_pointer((void *)thing, start_addr)) {
366 gc_assert(num_valid_stack_locations < MAX_STACK_POINTERS);
367 valid_stack_locations[num_valid_stack_locations++] = sp;
372 if (pointer_filter_verbose) {
373 fprintf(stderr, "number of valid stack pointers = %d\n",
374 num_valid_stack_locations);
375 fprintf(stderr, "number of stack return addresses = %d\n",
376 num_valid_stack_ra_locations);
381 pscav_i386_stack(void)
385 for (i = 0; i < num_valid_stack_locations; i++)
386 pscav(valid_stack_locations[i], 1, 0);
388 for (i = 0; i < num_valid_stack_ra_locations; i++) {
389 lispobj code_obj = (lispobj)valid_stack_ra_code_objects[i];
390 pscav(&code_obj, 1, 0);
391 if (pointer_filter_verbose) {
392 fprintf(stderr,"*C moved RA %x to %x; for code object %x to %x\n",
393 *valid_stack_ra_locations[i],
394 (int)(*valid_stack_ra_locations[i])
395 - ((int)valid_stack_ra_code_objects[i] - (int)code_obj),
396 (unsigned int) valid_stack_ra_code_objects[i], code_obj);
398 *valid_stack_ra_locations[i] =
399 ((int)(*valid_stack_ra_locations[i])
400 - ((int)valid_stack_ra_code_objects[i] - (int)code_obj));
408 pscav_later(lispobj *where, int count)
412 if (count > LATERMAXCOUNT) {
413 while (count > LATERMAXCOUNT) {
414 pscav_later(where, LATERMAXCOUNT);
415 count -= LATERMAXCOUNT;
416 where += LATERMAXCOUNT;
420 if (later_blocks == NULL || later_count == LATERBLOCKSIZE ||
421 (later_count == LATERBLOCKSIZE-1 && count > 1)) {
422 new = (struct later *)malloc(sizeof(struct later));
423 new->next = later_blocks;
424 if (later_blocks && later_count < LATERBLOCKSIZE)
425 later_blocks->u[later_count].ptr = NULL;
431 later_blocks->u[later_count++].count = count;
432 later_blocks->u[later_count++].ptr = where;
437 ptrans_boxed(lispobj thing, lispobj header, boolean constant)
440 lispobj result, *new, *old;
442 nwords = 1 + HeaderValue(header);
445 old = (lispobj *)PTR(thing);
447 new = read_only_free;
448 read_only_free += CEILING(nwords, 2);
452 static_free += CEILING(nwords, 2);
456 bcopy(old, new, nwords * sizeof(lispobj));
458 /* Deposit forwarding pointer. */
459 result = (lispobj)new | LowtagOf(thing);
463 pscav(new, nwords, constant);
468 /* We need to look at the layout to see whether it is a pure structure
469 * class, and only then can we transport as constant. If it is pure,
470 * we can ALWAYS transport as a constant. */
472 ptrans_instance(lispobj thing, lispobj header, boolean constant)
474 lispobj layout = ((struct instance *)PTR(thing))->slots[0];
475 lispobj pure = ((struct instance *)PTR(layout))->slots[15];
479 return (ptrans_boxed(thing, header, 1));
481 return (ptrans_boxed(thing, header, 0));
484 /* Substructure: special case for the COMPACT-INFO-ENVs,
485 * where the instance may have a point to the dynamic
486 * space placed into it (e.g. the cache-name slot), but
487 * the lists and arrays at the time of a purify can be
488 * moved to the RO space. */
490 lispobj result, *new, *old;
492 nwords = 1 + HeaderValue(header);
495 old = (lispobj *)PTR(thing);
497 static_free += CEILING(nwords, 2);
500 bcopy(old, new, nwords * sizeof(lispobj));
502 /* Deposit forwarding pointer. */
503 result = (lispobj)new | LowtagOf(thing);
507 pscav(new, nwords, 1);
513 return NIL; /* dummy value: return something ... */
518 ptrans_fdefn(lispobj thing, lispobj header)
521 lispobj result, *new, *old, oldfn;
524 nwords = 1 + HeaderValue(header);
527 old = (lispobj *)PTR(thing);
529 static_free += CEILING(nwords, 2);
532 bcopy(old, new, nwords * sizeof(lispobj));
534 /* Deposit forwarding pointer. */
535 result = (lispobj)new | LowtagOf(thing);
538 /* Scavenge the function. */
539 fdefn = (struct fdefn *)new;
540 oldfn = fdefn->function;
541 pscav(&fdefn->function, 1, 0);
542 if ((char *)oldfn + RAW_ADDR_OFFSET == fdefn->raw_addr)
543 fdefn->raw_addr = (char *)fdefn->function + RAW_ADDR_OFFSET;
549 ptrans_unboxed(lispobj thing, lispobj header)
552 lispobj result, *new, *old;
554 nwords = 1 + HeaderValue(header);
557 old = (lispobj *)PTR(thing);
558 new = read_only_free;
559 read_only_free += CEILING(nwords, 2);
562 bcopy(old, new, nwords * sizeof(lispobj));
564 /* Deposit forwarding pointer. */
565 result = (lispobj)new | LowtagOf(thing);
572 ptrans_vector(lispobj thing, int bits, int extra,
573 boolean boxed, boolean constant)
575 struct vector *vector;
577 lispobj result, *new;
579 vector = (struct vector *)PTR(thing);
580 nwords = 2 + (CEILING((fixnum_value(vector->length)+extra)*bits,32)>>5);
582 if (boxed && !constant) {
584 static_free += CEILING(nwords, 2);
587 new = read_only_free;
588 read_only_free += CEILING(nwords, 2);
591 bcopy(vector, new, nwords * sizeof(lispobj));
593 result = (lispobj)new | LowtagOf(thing);
594 vector->header = result;
597 pscav(new, nwords, constant);
604 apply_code_fixups_during_purify(struct code *old_code, struct code *new_code)
606 int nheader_words, ncode_words, nwords;
607 void *constants_start_addr, *constants_end_addr;
608 void *code_start_addr, *code_end_addr;
609 lispobj fixups = NIL;
610 unsigned displacement = (unsigned)new_code - (unsigned)old_code;
611 struct vector *fixups_vector;
613 /* Byte compiled code has no fixups. The trace table offset will be
614 * a fixnum if it's x86 compiled code - check. */
615 if (new_code->trace_table_offset & 0x3)
618 /* Else it's x86 machine code. */
619 ncode_words = fixnum_value(new_code->code_size);
620 nheader_words = HeaderValue(*(lispobj *)new_code);
621 nwords = ncode_words + nheader_words;
623 constants_start_addr = (void *)new_code + 5*4;
624 constants_end_addr = (void *)new_code + nheader_words*4;
625 code_start_addr = (void *)new_code + nheader_words*4;
626 code_end_addr = (void *)new_code + nwords*4;
628 /* The first constant should be a pointer to the fixups for this
629 * code objects. Check. */
630 fixups = new_code->constants[0];
632 /* It will be 0 or the unbound-marker if there are no fixups, and
633 * will be an other-pointer to a vector if it is valid. */
634 if ((fixups==0) || (fixups==type_UnboundMarker) || !Pointerp(fixups)) {
636 /* Check for a possible errors. */
637 sniff_code_object(new_code,displacement);
642 fixups_vector = (struct vector *)PTR(fixups);
644 /* Could be pointing to a forwarding pointer. */
645 if (Pointerp(fixups) && (dynamic_pointer_p(fixups))
646 && forwarding_pointer_p(*(lispobj *)fixups_vector)) {
647 /* If so then follow it. */
648 fixups_vector = (struct vector *)PTR(*(lispobj *)fixups_vector);
651 if (TypeOf(fixups_vector->header) == type_SimpleArrayUnsignedByte32) {
652 /* We got the fixups for the code block. Now work through the vector,
653 * and apply a fixup at each address. */
654 int length = fixnum_value(fixups_vector->length);
656 for (i=0; i<length; i++) {
657 unsigned offset = fixups_vector->data[i];
658 /* Now check the current value of offset. */
659 unsigned old_value = *(unsigned *)((unsigned)code_start_addr + offset);
661 /* If it's within the old_code object then it must be an
662 * absolute fixup (relative ones are not saved) */
663 if ((old_value>=(unsigned)old_code)
664 && (old_value<((unsigned)old_code + nwords*4)))
665 /* So add the dispacement. */
666 *(unsigned *)((unsigned)code_start_addr + offset) = old_value
669 /* It is outside the old code object so it must be a relative
670 * fixup (absolute fixups are not saved). So subtract the
672 *(unsigned *)((unsigned)code_start_addr + offset) = old_value
677 /* No longer need the fixups. */
678 new_code->constants[0] = 0;
681 /* Check for possible errors. */
682 sniff_code_object(new_code,displacement);
688 ptrans_code(lispobj thing)
690 struct code *code, *new;
692 lispobj func, result;
694 code = (struct code *)PTR(thing);
695 nwords = HeaderValue(code->header) + fixnum_value(code->code_size);
697 new = (struct code *)read_only_free;
698 read_only_free += CEILING(nwords, 2);
700 bcopy(code, new, nwords * sizeof(lispobj));
703 apply_code_fixups_during_purify(code,new);
706 result = (lispobj)new | type_OtherPointer;
708 /* Stick in a forwarding pointer for the code object. */
709 *(lispobj *)code = result;
711 /* Put in forwarding pointers for all the functions. */
712 for (func = code->entry_points;
714 func = ((struct function *)PTR(func))->next) {
716 gc_assert(LowtagOf(func) == type_FunctionPointer);
718 *(lispobj *)PTR(func) = result + (func - thing);
721 /* Arrange to scavenge the debug info later. */
722 pscav_later(&new->debug_info, 1);
724 if(new->trace_table_offset & 0x3)
726 pscav(&new->trace_table_offset, 1, 0);
728 new->trace_table_offset = NIL; /* limit lifetime */
731 /* Scavenge the constants. */
732 pscav(new->constants, HeaderValue(new->header)-5, 1);
734 /* Scavenge all the functions. */
735 pscav(&new->entry_points, 1, 1);
736 for (func = new->entry_points;
738 func = ((struct function *)PTR(func))->next) {
739 gc_assert(LowtagOf(func) == type_FunctionPointer);
740 gc_assert(!dynamic_pointer_p(func));
743 /* Temporarly convert the self pointer to a real function
745 ((struct function *)PTR(func))->self -= RAW_ADDR_OFFSET;
747 pscav(&((struct function *)PTR(func))->self, 2, 1);
749 ((struct function *)PTR(func))->self += RAW_ADDR_OFFSET;
751 pscav_later(&((struct function *)PTR(func))->name, 3);
758 ptrans_func(lispobj thing, lispobj header)
761 lispobj code, *new, *old, result;
762 struct function *function;
764 /* Thing can either be a function header, a closure function
765 * header, a closure, or a funcallable-instance. If it's a closure
766 * or a funcallable-instance, we do the same as ptrans_boxed.
767 * Otherwise we have to do something strange, 'cause it is buried
768 * inside a code object. */
770 if (TypeOf(header) == type_FunctionHeader ||
771 TypeOf(header) == type_ClosureFunctionHeader) {
773 /* We can only end up here if the code object has not been
774 * scavenged, because if it had been scavenged, forwarding pointers
775 * would have been left behind for all the entry points. */
777 function = (struct function *)PTR(thing);
778 code = (PTR(thing)-(HeaderValue(function->header)*sizeof(lispobj))) |
781 /* This will cause the function's header to be replaced with a
782 * forwarding pointer. */
785 /* So we can just return that. */
786 return function->header;
789 /* It's some kind of closure-like thing. */
790 nwords = 1 + HeaderValue(header);
791 old = (lispobj *)PTR(thing);
793 /* Allocate the new one. */
794 if (TypeOf(header) == type_FuncallableInstanceHeader) {
795 /* FINs *must* not go in read_only space. */
797 static_free += CEILING(nwords, 2);
800 /* Closures can always go in read-only space, 'cause they
803 new = read_only_free;
804 read_only_free += CEILING(nwords, 2);
807 bcopy(old, new, nwords * sizeof(lispobj));
809 /* Deposit forwarding pointer. */
810 result = (lispobj)new | LowtagOf(thing);
814 pscav(new, nwords, 0);
821 ptrans_returnpc(lispobj thing, lispobj header)
825 /* Find the corresponding code object. */
826 code = thing - HeaderValue(header)*sizeof(lispobj);
828 /* Make sure it's been transported. */
829 new = *(lispobj *)PTR(code);
830 if (!forwarding_pointer_p(new))
831 new = ptrans_code(code);
833 /* Maintain the offset: */
834 return new + (thing - code);
837 #define WORDS_PER_CONS CEILING(sizeof(struct cons) / sizeof(lispobj), 2)
840 ptrans_list(lispobj thing, boolean constant)
842 struct cons *old, *new, *orig;
846 orig = (struct cons *)read_only_free;
848 orig = (struct cons *)static_free;
852 /* Allocate a new cons cell. */
853 old = (struct cons *)PTR(thing);
855 new = (struct cons *)read_only_free;
856 read_only_free += WORDS_PER_CONS;
859 new = (struct cons *)static_free;
860 static_free += WORDS_PER_CONS;
863 /* Copy the cons cell and keep a pointer to the cdr. */
865 thing = new->cdr = old->cdr;
867 /* Set up the forwarding pointer. */
868 *(lispobj *)old = ((lispobj)new) | type_ListPointer;
870 /* And count this cell. */
872 } while (LowtagOf(thing) == type_ListPointer &&
873 dynamic_pointer_p(thing) &&
874 !(forwarding_pointer_p(*(lispobj *)PTR(thing))));
876 /* Scavenge the list we just copied. */
877 pscav((lispobj *)orig, length * WORDS_PER_CONS, constant);
879 return ((lispobj)orig) | type_ListPointer;
883 ptrans_otherptr(lispobj thing, lispobj header, boolean constant)
885 switch (TypeOf(header)) {
887 case type_SingleFloat:
888 case type_DoubleFloat:
889 #ifdef type_LongFloat
892 #ifdef type_ComplexSingleFloat
893 case type_ComplexSingleFloat:
895 #ifdef type_ComplexDoubleFloat
896 case type_ComplexDoubleFloat:
898 #ifdef type_ComplexLongFloat
899 case type_ComplexLongFloat:
902 return ptrans_unboxed(thing, header);
906 case type_SimpleArray:
907 case type_ComplexString:
908 case type_ComplexVector:
909 case type_ComplexArray:
910 return ptrans_boxed(thing, header, constant);
912 case type_ValueCellHeader:
913 case type_WeakPointer:
914 return ptrans_boxed(thing, header, 0);
916 case type_SymbolHeader:
917 return ptrans_boxed(thing, header, 0);
919 case type_SimpleString:
920 return ptrans_vector(thing, 8, 1, 0, constant);
922 case type_SimpleBitVector:
923 return ptrans_vector(thing, 1, 0, 0, constant);
925 case type_SimpleVector:
926 return ptrans_vector(thing, 32, 0, 1, constant);
928 case type_SimpleArrayUnsignedByte2:
929 return ptrans_vector(thing, 2, 0, 0, constant);
931 case type_SimpleArrayUnsignedByte4:
932 return ptrans_vector(thing, 4, 0, 0, constant);
934 case type_SimpleArrayUnsignedByte8:
935 #ifdef type_SimpleArraySignedByte8
936 case type_SimpleArraySignedByte8:
938 return ptrans_vector(thing, 8, 0, 0, constant);
940 case type_SimpleArrayUnsignedByte16:
941 #ifdef type_SimpleArraySignedByte16
942 case type_SimpleArraySignedByte16:
944 return ptrans_vector(thing, 16, 0, 0, constant);
946 case type_SimpleArrayUnsignedByte32:
947 #ifdef type_SimpleArraySignedByte30
948 case type_SimpleArraySignedByte30:
950 #ifdef type_SimpleArraySignedByte32
951 case type_SimpleArraySignedByte32:
953 return ptrans_vector(thing, 32, 0, 0, constant);
955 case type_SimpleArraySingleFloat:
956 return ptrans_vector(thing, 32, 0, 0, constant);
958 case type_SimpleArrayDoubleFloat:
959 return ptrans_vector(thing, 64, 0, 0, constant);
961 #ifdef type_SimpleArrayLongFloat
962 case type_SimpleArrayLongFloat:
964 return ptrans_vector(thing, 96, 0, 0, constant);
967 return ptrans_vector(thing, 128, 0, 0, constant);
971 #ifdef type_SimpleArrayComplexSingleFloat
972 case type_SimpleArrayComplexSingleFloat:
973 return ptrans_vector(thing, 64, 0, 0, constant);
976 #ifdef type_SimpleArrayComplexDoubleFloat
977 case type_SimpleArrayComplexDoubleFloat:
978 return ptrans_vector(thing, 128, 0, 0, constant);
981 #ifdef type_SimpleArrayComplexLongFloat
982 case type_SimpleArrayComplexLongFloat:
984 return ptrans_vector(thing, 192, 0, 0, constant);
987 return ptrans_vector(thing, 256, 0, 0, constant);
991 case type_CodeHeader:
992 return ptrans_code(thing);
994 case type_ReturnPcHeader:
995 return ptrans_returnpc(thing, header);
998 return ptrans_fdefn(thing, header);
1001 /* Should only come across other pointers to the above stuff. */
1008 pscav_fdefn(struct fdefn *fdefn)
1012 fix_func = ((char *)(fdefn->function+RAW_ADDR_OFFSET) == fdefn->raw_addr);
1013 pscav(&fdefn->name, 1, 1);
1014 pscav(&fdefn->function, 1, 0);
1016 fdefn->raw_addr = (char *)(fdefn->function + RAW_ADDR_OFFSET);
1017 return sizeof(struct fdefn) / sizeof(lispobj);
1021 /* now putting code objects in static space */
1023 pscav_code(struct code*code)
1027 nwords = HeaderValue(code->header) + fixnum_value(code->code_size);
1029 /* pw--The trace_table_offset slot can contain a list pointer. This
1030 * occurs when the code object is a top level form that initializes
1031 * a byte-compiled function. The fact that PURIFY was ignoring this
1032 * slot may be a bug unrelated to the x86 port, except that TLF's
1033 * normally become unreachable after the loader calls them and
1034 * won't be seen by PURIFY at all!! */
1035 if(code->trace_table_offset & 0x3)
1037 pscav(&code->trace_table_offset, 1, 0);
1039 code->trace_table_offset = NIL; /* limit lifetime */
1042 /* Arrange to scavenge the debug info later. */
1043 pscav_later(&code->debug_info, 1);
1045 /* Scavenge the constants. */
1046 pscav(code->constants, HeaderValue(code->header)-5, 1);
1048 /* Scavenge all the functions. */
1049 pscav(&code->entry_points, 1, 1);
1050 for (func = code->entry_points;
1052 func = ((struct function *)PTR(func))->next) {
1053 gc_assert(LowtagOf(func) == type_FunctionPointer);
1054 gc_assert(!dynamic_pointer_p(func));
1057 /* Temporarly convert the self pointer to a real function
1059 ((struct function *)PTR(func))->self -= RAW_ADDR_OFFSET;
1061 pscav(&((struct function *)PTR(func))->self, 2, 1);
1063 ((struct function *)PTR(func))->self += RAW_ADDR_OFFSET;
1065 pscav_later(&((struct function *)PTR(func))->name, 3);
1068 return CEILING(nwords,2);
1073 pscav(lispobj *addr, int nwords, boolean constant)
1075 lispobj thing, *thingp, header;
1076 int count = 0; /* (0 = dummy init value to stop GCC warning) */
1077 struct vector *vector;
1079 while (nwords > 0) {
1081 if (Pointerp(thing)) {
1082 /* It's a pointer. Is it something we might have to move? */
1083 if (dynamic_pointer_p(thing)) {
1084 /* Maybe. Have we already moved it? */
1085 thingp = (lispobj *)PTR(thing);
1087 if (Pointerp(header) && forwarding_pointer_p(header))
1088 /* Yep, so just copy the forwarding pointer. */
1091 /* Nope, copy the object. */
1092 switch (LowtagOf(thing)) {
1093 case type_FunctionPointer:
1094 thing = ptrans_func(thing, header);
1097 case type_ListPointer:
1098 thing = ptrans_list(thing, constant);
1101 case type_InstancePointer:
1102 thing = ptrans_instance(thing, header, constant);
1105 case type_OtherPointer:
1106 thing = ptrans_otherptr(thing, header, constant);
1110 /* It was a pointer, but not one of them? */
1118 else if (thing & 3) {
1119 /* It's an other immediate. Maybe the header for an unboxed */
1121 switch (TypeOf(thing)) {
1123 case type_SingleFloat:
1124 case type_DoubleFloat:
1125 #ifdef type_LongFloat
1126 case type_LongFloat:
1129 /* It's an unboxed simple object. */
1130 count = HeaderValue(thing)+1;
1133 case type_SimpleVector:
1134 if (HeaderValue(thing) == subtype_VectorValidHashing)
1135 *addr = (subtype_VectorMustRehash<<type_Bits) |
1140 case type_SimpleString:
1141 vector = (struct vector *)addr;
1142 count = CEILING(NWORDS(fixnum_value(vector->length)+1,4)+2,2);
1145 case type_SimpleBitVector:
1146 vector = (struct vector *)addr;
1147 count = CEILING(NWORDS(fixnum_value(vector->length),32)+2,2);
1150 case type_SimpleArrayUnsignedByte2:
1151 vector = (struct vector *)addr;
1152 count = CEILING(NWORDS(fixnum_value(vector->length),16)+2,2);
1155 case type_SimpleArrayUnsignedByte4:
1156 vector = (struct vector *)addr;
1157 count = CEILING(NWORDS(fixnum_value(vector->length),8)+2,2);
1160 case type_SimpleArrayUnsignedByte8:
1161 #ifdef type_SimpleArraySignedByte8
1162 case type_SimpleArraySignedByte8:
1164 vector = (struct vector *)addr;
1165 count = CEILING(NWORDS(fixnum_value(vector->length),4)+2,2);
1168 case type_SimpleArrayUnsignedByte16:
1169 #ifdef type_SimpleArraySignedByte16
1170 case type_SimpleArraySignedByte16:
1172 vector = (struct vector *)addr;
1173 count = CEILING(NWORDS(fixnum_value(vector->length),2)+2,2);
1176 case type_SimpleArrayUnsignedByte32:
1177 #ifdef type_SimpleArraySignedByte30
1178 case type_SimpleArraySignedByte30:
1180 #ifdef type_SimpleArraySignedByte32
1181 case type_SimpleArraySignedByte32:
1183 vector = (struct vector *)addr;
1184 count = CEILING(fixnum_value(vector->length)+2,2);
1187 case type_SimpleArraySingleFloat:
1188 vector = (struct vector *)addr;
1189 count = CEILING(fixnum_value(vector->length)+2,2);
1192 case type_SimpleArrayDoubleFloat:
1193 #ifdef type_SimpleArrayComplexSingleFloat
1194 case type_SimpleArrayComplexSingleFloat:
1196 vector = (struct vector *)addr;
1197 count = fixnum_value(vector->length)*2+2;
1200 #ifdef type_SimpleArrayLongFloat
1201 case type_SimpleArrayLongFloat:
1202 vector = (struct vector *)addr;
1204 count = fixnum_value(vector->length)*3+2;
1207 count = fixnum_value(vector->length)*4+2;
1212 #ifdef type_SimpleArrayComplexDoubleFloat
1213 case type_SimpleArrayComplexDoubleFloat:
1214 vector = (struct vector *)addr;
1215 count = fixnum_value(vector->length)*4+2;
1219 #ifdef type_SimpleArrayComplexLongFloat
1220 case type_SimpleArrayComplexLongFloat:
1221 vector = (struct vector *)addr;
1223 count = fixnum_value(vector->length)*6+2;
1226 count = fixnum_value(vector->length)*8+2;
1231 case type_CodeHeader:
1233 gc_abort(); /* no code headers in static space */
1235 count = pscav_code((struct code*)addr);
1239 case type_FunctionHeader:
1240 case type_ClosureFunctionHeader:
1241 case type_ReturnPcHeader:
1242 /* We should never hit any of these, 'cause they occur
1243 * buried in the middle of code objects. */
1248 case type_ClosureHeader:
1249 case type_FuncallableInstanceHeader:
1250 case type_ByteCodeFunction:
1251 case type_ByteCodeClosure:
1252 /* The function self pointer needs special care on the
1253 * x86 because it is the real entry point. */
1255 lispobj fun = ((struct closure *)addr)->function
1257 pscav(&fun, 1, constant);
1258 ((struct closure *)addr)->function = fun + RAW_ADDR_OFFSET;
1264 case type_WeakPointer:
1265 /* Weak pointers get preserved during purify, 'cause I
1266 * don't feel like figuring out how to break them. */
1267 pscav(addr+1, 2, constant);
1272 /* We have to handle fdefn objects specially, so we
1273 * can fix up the raw function address. */
1274 count = pscav_fdefn((struct fdefn *)addr);
1283 /* It's a fixnum. */
1295 purify(lispobj static_roots, lispobj read_only_roots)
1299 struct later *laters, *next;
1302 printf("[doing purification:");
1306 if (fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX)) != 0) {
1307 /* FIXME: 1. What does this mean? 2. It shouldn't be reporting
1308 * its error simply by a. printing a string b. to stdout instead
1310 printf(" Ack! Can't purify interrupt contexts. ");
1315 #if defined(__i386__)
1316 dynamic_space_free_pointer =
1317 (lispobj*)SymbolValue(ALLOCATION_POINTER);
1320 read_only_end = read_only_free =
1321 (lispobj *)SymbolValue(READ_ONLY_SPACE_FREE_POINTER);
1322 static_end = static_free =
1323 (lispobj *)SymbolValue(STATIC_SPACE_FREE_POINTER);
1331 gc_assert((lispobj *)CONTROL_STACK_END > ((&read_only_roots)+1));
1332 setup_i386_stack_scav(((&static_roots)-2), (lispobj *)CONTROL_STACK_END);
1335 pscav(&static_roots, 1, 0);
1336 pscav(&read_only_roots, 1, 1);
1339 printf(" handlers");
1342 pscav((lispobj *) interrupt_handlers,
1343 sizeof(interrupt_handlers) / sizeof(lispobj),
1351 pscav((lispobj *)CONTROL_STACK_START,
1352 current_control_stack_pointer - (lispobj *)CONTROL_STACK_START,
1361 printf(" bindings");
1364 #if !defined(__i386__)
1365 pscav( (lispobj *)BINDING_STACK_START,
1366 (lispobj *)current_binding_stack_pointer - (lispobj *)BINDING_STACK_START,
1369 pscav( (lispobj *)BINDING_STACK_START,
1370 (lispobj *)SymbolValue(BINDING_STACK_POINTER) -
1371 (lispobj *)BINDING_STACK_START,
1375 /* The original CMU CL code had scavenge-read-only-space code
1376 * controlled by the Lisp-level variable
1377 * *SCAVENGE-READ-ONLY-SPACE*. It was disabled by default, and it
1378 * wasn't documented under what circumstances it was useful or
1379 * safe to turn it on, so it's been turned off in SBCL. If you
1380 * want/need this functionality, and can test and document it,
1381 * please submit a patch. */
1383 if (SymbolValue(SCAVENGE_READ_ONLY_SPACE) != type_UnboundMarker
1384 && SymbolValue(SCAVENGE_READ_ONLY_SPACE) != NIL) {
1385 unsigned read_only_space_size =
1386 (lispobj *)SymbolValue(READ_ONLY_SPACE_FREE_POINTER) -
1387 (lispobj *)READ_ONLY_SPACE_START;
1389 "scavenging read only space: %d bytes\n",
1390 read_only_space_size * sizeof(lispobj));
1391 pscav( (lispobj *)READ_ONLY_SPACE_START, read_only_space_size, 0);
1399 clean = (lispobj *)STATIC_SPACE_START;
1401 while (clean != static_free)
1402 clean = pscav(clean, static_free - clean, 0);
1403 laters = later_blocks;
1404 count = later_count;
1405 later_blocks = NULL;
1407 while (laters != NULL) {
1408 for (i = 0; i < count; i++) {
1409 if (laters->u[i].count == 0) {
1411 } else if (laters->u[i].count <= LATERMAXCOUNT) {
1412 pscav(laters->u[i+1].ptr, laters->u[i].count, 1);
1415 pscav(laters->u[i].ptr, 1, 1);
1418 next = laters->next;
1421 count = LATERBLOCKSIZE;
1423 } while (clean != static_free || later_blocks != NULL);
1430 os_zero((os_vm_address_t) current_dynamic_space,
1431 (os_vm_size_t) DYNAMIC_SPACE_SIZE);
1433 /* Zero the stack. Note that the stack is also zeroed by SUB-GC
1434 * calling SCRUB-CONTROL-STACK - this zeros the stack on the x86. */
1436 os_zero((os_vm_address_t) current_control_stack_pointer,
1437 (os_vm_size_t) (CONTROL_STACK_SIZE -
1438 ((current_control_stack_pointer -
1439 (lispobj *)CONTROL_STACK_START) *
1443 /* It helps to update the heap free pointers so that free_heap can
1444 * verify after it's done. */
1445 SetSymbolValue(READ_ONLY_SPACE_FREE_POINTER, (lispobj)read_only_free);
1446 SetSymbolValue(STATIC_SPACE_FREE_POINTER, (lispobj)static_free);
1448 #if !defined(__i386__)
1449 dynamic_space_free_pointer = current_dynamic_space;
1454 #error unsupported case /* in CMU CL, was "ibmrt using GC" */