4 * Copyright 2004-2009 by Marc Feeley and Vincent St-Amour, All Rights Reserved.
7 #include "picobit-vm.h"
9 void init_ram_heap () {
11 obj o
= MAX_RAM_ENCODING
;
12 uint16 bound
= MIN_RAM_ENCODING
+ ((glovars
+ 1) >> 1);
17 // we don't want to add globals to the free list, and globals occupy the
18 // beginning of memory at the rate of 2 globals per word (car and cdr)
19 ram_set_gc_tags (o
, GC_TAG_UNMARKED
);
20 ram_set_car (o
, free_list
);
25 free_list_vec
= MIN_VEC_ENCODING
;
26 ram_set_car (free_list_vec
, 0);
27 // each node of the free list must know the free length that follows it
28 // this free length is stored in words, not in bytes
29 // if we did count in bytes, the number might need more than 13 bits
30 ram_set_cdr (free_list_vec
, (VEC_BYTES
>> 2));
32 for (i
=0; i
<glovars
; i
++)
33 set_global (i
, OBJ_FALSE
);
44 void mark (obj temp
) {
58 IF_GC_TRACE(printf ("push stack=%d visit=%d (tag=%d)\n", stack
, visit
, ram_get_gc_tags (visit
)>>5));
60 if ((HAS_1_OBJECT_FIELD (visit
) && ram_get_gc_tag0 (visit
))
61 || (HAS_2_OBJECT_FIELDS (visit
)
62 && (ram_get_gc_tags (visit
) != GC_TAG_UNMARKED
)))
63 IF_GC_TRACE(printf ("case 1\n"));
65 if (HAS_2_OBJECT_FIELDS(visit
)) { // pairs and continuations
66 IF_GC_TRACE(printf ("case 2\n"));
70 temp
= ram_get_cdr (visit
);
73 IF_GC_TRACE(printf ("case 3\n"));
74 ram_set_gc_tags (visit
, GC_TAG_1_LEFT
);
75 ram_set_cdr (visit
, stack
);
79 IF_GC_TRACE(printf ("case 4\n"));
84 if (HAS_1_OBJECT_FIELD(visit
)) {
85 IF_GC_TRACE(printf ("case 5\n"));
89 temp
= ram_get_car (visit
);
92 IF_GC_TRACE(printf ("case 6\n"));
93 ram_set_gc_tag0 (visit
, GC_TAG_0_LEFT
);
94 ram_set_car (visit
, stack
);
99 IF_GC_TRACE(printf ("case 7\n"));
102 IF_GC_TRACE(printf ("case 8\n"));
104 ram_set_gc_tag0 (visit
, GC_TAG_0_LEFT
);
109 IF_GC_TRACE(printf ("pop stack=%d visit=%d (tag=%d)\n", stack
, visit
, ram_get_gc_tags (visit
)>>6));
112 if (HAS_2_OBJECT_FIELDS(stack
) && ram_get_gc_tag1 (stack
)) {
113 IF_GC_TRACE(printf ("case 9\n"));
115 temp
= ram_get_cdr (stack
); /* pop through cdr */
116 ram_set_cdr (stack
, visit
);
120 ram_set_gc_tag1(visit
, GC_TAG_UNMARKED
);
121 // we unset the "1-left" bit
126 IF_GC_TRACE(printf ("case 11\n"));
128 temp
= ram_get_car (stack
); /* pop through car */
129 ram_set_car (stack
, visit
);
149 obj visit
= MAX_RAM_ENCODING
;
153 while (visit
>= (MIN_RAM_ENCODING
+ ((glovars
+ 1) >> 1))) {
154 // we don't want to sweep the global variables area
155 if ((RAM_COMPOSITE(visit
)
156 && (ram_get_gc_tags (visit
) == GC_TAG_UNMARKED
)) // 2 mark bit
157 || !(ram_get_gc_tags (visit
) & GC_TAG_0_LEFT
)) { // 1 mark bit
159 if (RAM_VECTOR(visit
)) {
160 // when we sweep a vector, we also have to sweep its contents
161 obj o
= ram_get_cdr (visit
);
162 uint16 i
= ram_get_car (visit
); // number of elements
163 ram_set_car (o
, free_list_vec
);
164 ram_set_cdr (o
, ((i
+ 3) >> 2)); // free length, in words
166 // TODO merge free spaces
168 ram_set_car (visit
, free_list
);
172 if (RAM_COMPOSITE(visit
))
173 ram_set_gc_tags (visit
, GC_TAG_UNMARKED
);
174 else // only 1 mark bit to unset
175 ram_set_gc_tag0 (visit
, GC_TAG_UNMARKED
);
186 printf ("**************** memory needed = %d\n", max_live
+1);
195 IF_TRACE(printf("\nGC BEGINS\n"));
197 IF_GC_TRACE(printf("arg1\n"));
199 IF_GC_TRACE(printf("arg2\n"));
201 IF_GC_TRACE(printf("arg3\n"));
203 IF_GC_TRACE(printf("arg4\n"));
205 IF_GC_TRACE(printf("cont\n"));
207 IF_GC_TRACE(printf("env\n"));
210 IF_GC_TRACE(printf("globals\n"));
211 for (i
=0; i
<glovars
; i
++)
212 mark (get_global (i
));
217 obj
alloc_ram_cell () {
224 if (free_list
== 0) {
229 ERROR("alloc_ram_cell", "memory is full");
234 free_list
= ram_get_car (o
);
239 obj
alloc_ram_cell_init (uint8 f0
, uint8 f1
, uint8 f2
, uint8 f3
) {
240 obj o
= alloc_ram_cell ();
242 ram_set_field0 (o
, f0
);
243 ram_set_field1 (o
, f1
);
244 ram_set_field2 (o
, f2
);
245 ram_set_field3 (o
, f3
);
250 obj
alloc_vec_cell (uint16 n
) {
251 obj o
= free_list_vec
;
260 while ((ram_get_cdr (o
) * 4) < n
) { // free space too small
261 if (o
== 0) { // no free space, or none big enough
262 if (gc_done
) // we gc'd, but no space is big enough for the vector
263 ERROR("alloc_vec_cell", "no room for vector");
271 } // TODO merge adjacent free spaces, maybe compact ?
276 // case 1 : the new vector fills every free word advertized, we remove the
277 // node from the free list
278 if (((ram_get_cdr(o
) * 4) - n
) < 4) {
280 ram_set_car (prec
, ram_get_car (o
));
282 free_list_vec
= ram_get_car (o
);
284 // case 2 : there is still some space left in the free section, create a new
285 // node to represent this space
287 obj new_free
= o
+ ((n
+ 3) >> 2);
289 ram_set_car (prec
, new_free
);
291 free_list_vec
= new_free
;
292 ram_set_car (new_free
, ram_get_car (o
));
293 ram_set_cdr (new_free
, ram_get_cdr (o
) - ((n
+ 3) >> 2));