]> gitweb.factorcode.org Git - factor.git/blob - vm/slot_visitor.hpp
GC maps for more compact inline GC checks
[factor.git] / vm / slot_visitor.hpp
1 namespace factor
2 {
3
4 /* Size of the object pointed to by an untagged pointer */
5 template<typename Fixup>
6 cell object::size(Fixup fixup) const
7 {
8         if(free_p()) return ((free_heap_block *)this)->size();
9
10         switch(type())
11         {
12         case ARRAY_TYPE:
13                 return align(array_size((array*)this),data_alignment);
14         case BIGNUM_TYPE:
15                 return align(array_size((bignum*)this),data_alignment);
16         case BYTE_ARRAY_TYPE:
17                 return align(array_size((byte_array*)this),data_alignment);
18         case STRING_TYPE:
19                 return align(string_size(string_capacity((string*)this)),data_alignment);
20         case TUPLE_TYPE:
21                 {
22                         tuple_layout *layout = (tuple_layout *)fixup.translate_data(untag<object>(((tuple *)this)->layout));
23                         return align(tuple_size(layout),data_alignment);
24                 }
25         case QUOTATION_TYPE:
26                 return align(sizeof(quotation),data_alignment);
27         case WORD_TYPE:
28                 return align(sizeof(word),data_alignment);
29         case FLOAT_TYPE:
30                 return align(sizeof(boxed_float),data_alignment);
31         case DLL_TYPE:
32                 return align(sizeof(dll),data_alignment);
33         case ALIEN_TYPE:
34                 return align(sizeof(alien),data_alignment);
35         case WRAPPER_TYPE:
36                 return align(sizeof(wrapper),data_alignment);
37         case CALLSTACK_TYPE:
38                 return align(callstack_object_size(untag_fixnum(((callstack *)this)->length)),data_alignment);
39         default:
40                 critical_error("Invalid header in size",(cell)this);
41                 return 0; /* can't happen */
42         }
43 }
44
45 inline cell object::size() const
46 {
47         return size(no_fixup());
48 }
49
50 /* The number of cells from the start of the object which should be scanned by
51 the GC. Some types have a binary payload at the end (string, word, DLL) which
52 we ignore. */
53 template<typename Fixup>
54 cell object::binary_payload_start(Fixup fixup) const
55 {
56         if(free_p()) return 0;
57
58         switch(type())
59         {
60         /* these objects do not refer to other objects at all */
61         case FLOAT_TYPE:
62         case BYTE_ARRAY_TYPE:
63         case BIGNUM_TYPE:
64         case CALLSTACK_TYPE:
65                 return 0;
66         /* these objects have some binary data at the end */
67         case WORD_TYPE:
68                 return sizeof(word) - sizeof(cell) * 3;
69         case ALIEN_TYPE:
70                 return sizeof(cell) * 3;
71         case DLL_TYPE:
72                 return sizeof(cell) * 2;
73         case QUOTATION_TYPE:
74                 return sizeof(quotation) - sizeof(cell) * 2;
75         case STRING_TYPE:
76                 return sizeof(string);
77         /* everything else consists entirely of pointers */
78         case ARRAY_TYPE:
79                 return array_size<array>(array_capacity((array*)this));
80         case TUPLE_TYPE:
81                 {
82                         tuple_layout *layout = (tuple_layout *)fixup.translate_data(untag<object>(((tuple *)this)->layout));
83                         return tuple_size(layout);
84                 }
85         case WRAPPER_TYPE:
86                 return sizeof(wrapper);
87         default:
88                 critical_error("Invalid header in binary_payload_start",(cell)this);
89                 return 0; /* can't happen */
90         }
91 }
92
93 inline cell object::binary_payload_start() const
94 {
95         return binary_payload_start(no_fixup());
96 }
97
98 /* Slot visitors iterate over the slots of an object, applying a functor to
99 each one that is a non-immediate slot. The pointer is untagged first. The
100 functor returns a new untagged object pointer. The return value may or may not equal the old one,
101 however the new pointer receives the same tag before being stored back to the
102 original location.
103
104 Slots storing immediate values are left unchanged and the visitor does inspect
105 them.
106
107 This is used by GC's copying, sweep and compact phases, and the implementation
108 of the become primitive.
109
110 Iteration is driven by visit_*() methods. Some of them define GC roots:
111 - visit_roots()
112 - visit_contexts() */
113
114 template<typename Fixup> struct slot_visitor {
115         factor_vm *parent;
116         Fixup fixup;
117
118         explicit slot_visitor<Fixup>(factor_vm *parent_, Fixup fixup_) :
119                 parent(parent_), fixup(fixup_) {}
120
121         cell visit_pointer(cell pointer);
122         void visit_handle(cell *handle);
123         void visit_object_array(cell *start, cell *end);
124         void visit_slots(object *ptr, cell payload_start);
125         void visit_slots(object *ptr);
126         void visit_stack_elements(segment *region, cell *top);
127         void visit_data_roots();
128         void visit_bignum_roots();
129         void visit_callback_roots();
130         void visit_literal_table_roots();
131         void visit_roots();
132         void visit_callstack_object(callstack *stack);
133         void visit_callstack(context *ctx);
134         void visit_contexts();
135         void visit_code_block_objects(code_block *compiled);
136         void visit_embedded_literals(code_block *compiled);
137 };
138
139 template<typename Fixup>
140 cell slot_visitor<Fixup>::visit_pointer(cell pointer)
141 {
142         if(immediate_p(pointer)) return pointer;
143
144         object *untagged = fixup.fixup_data(untag<object>(pointer));
145         return RETAG(untagged,TAG(pointer));
146 }
147
148 template<typename Fixup>
149 void slot_visitor<Fixup>::visit_handle(cell *handle)
150 {
151         *handle = visit_pointer(*handle);
152 }
153
154 template<typename Fixup>
155 void slot_visitor<Fixup>::visit_object_array(cell *start, cell *end)
156 {
157         while(start < end) visit_handle(start++);
158 }
159
160 template<typename Fixup>
161 void slot_visitor<Fixup>::visit_slots(object *ptr, cell payload_start)
162 {
163         cell *slot = (cell *)ptr;
164         cell *end = (cell *)((cell)ptr + payload_start);
165
166         if(slot != end)
167         {
168                 slot++;
169                 visit_object_array(slot,end);
170         }
171 }
172
173 template<typename Fixup>
174 void slot_visitor<Fixup>::visit_slots(object *obj)
175 {
176         if(obj->type() == CALLSTACK_TYPE)
177                 visit_callstack_object((callstack *)obj);
178         else
179                 visit_slots(obj,obj->binary_payload_start(fixup));
180 }
181
182 template<typename Fixup>
183 void slot_visitor<Fixup>::visit_stack_elements(segment *region, cell *top)
184 {
185         visit_object_array((cell *)region->start,top + 1);
186 }
187
188 template<typename Fixup>
189 void slot_visitor<Fixup>::visit_data_roots()
190 {
191         std::vector<data_root_range>::const_iterator iter = parent->data_roots.begin();
192         std::vector<data_root_range>::const_iterator end = parent->data_roots.end();
193
194         for(; iter < end; iter++)
195                 visit_object_array(iter->start,iter->start + iter->len);
196 }
197
198 template<typename Fixup>
199 void slot_visitor<Fixup>::visit_bignum_roots()
200 {
201         std::vector<cell>::const_iterator iter = parent->bignum_roots.begin();
202         std::vector<cell>::const_iterator end = parent->bignum_roots.end();
203
204         for(; iter < end; iter++)
205         {
206                 cell *handle = (cell *)(*iter);
207
208                 if(*handle)
209                         *handle = (cell)fixup.fixup_data(*(object **)handle);
210         }
211 }
212
213 template<typename Fixup>
214 struct callback_slot_visitor {
215         callback_heap *callbacks;
216         slot_visitor<Fixup> *visitor;
217
218         explicit callback_slot_visitor(callback_heap *callbacks_, slot_visitor<Fixup> *visitor_) :
219                 callbacks(callbacks_), visitor(visitor_) {}
220
221         void operator()(code_block *stub)
222         {
223                 visitor->visit_handle(&stub->owner);
224         }
225 };
226
227 template<typename Fixup>
228 void slot_visitor<Fixup>::visit_callback_roots()
229 {
230         callback_slot_visitor<Fixup> callback_visitor(parent->callbacks,this);
231         parent->callbacks->each_callback(callback_visitor);
232 }
233
234 template<typename Fixup>
235 void slot_visitor<Fixup>::visit_literal_table_roots()
236 {
237         std::map<code_block *, cell> *uninitialized_blocks = &parent->code->uninitialized_blocks;
238         std::map<code_block *, cell>::const_iterator iter = uninitialized_blocks->begin();
239         std::map<code_block *, cell>::const_iterator end = uninitialized_blocks->end();
240
241         std::map<code_block *, cell> new_uninitialized_blocks;
242         for(; iter != end; iter++)
243         {
244                 new_uninitialized_blocks.insert(std::make_pair(
245                         iter->first,
246                         visit_pointer(iter->second)));
247         }
248
249         parent->code->uninitialized_blocks = new_uninitialized_blocks;
250 }
251
252 template<typename Fixup>
253 void slot_visitor<Fixup>::visit_roots()
254 {
255         visit_handle(&parent->true_object);
256         visit_handle(&parent->bignum_zero);
257         visit_handle(&parent->bignum_pos_one);
258         visit_handle(&parent->bignum_neg_one);
259
260         visit_data_roots();
261         visit_bignum_roots();
262         visit_callback_roots();
263         visit_literal_table_roots();
264
265         visit_object_array(parent->special_objects,parent->special_objects + special_object_count);
266 }
267
268 template<typename Fixup>
269 struct call_frame_slot_visitor {
270         factor_vm *parent;
271         slot_visitor<Fixup> *visitor;
272
273         explicit call_frame_slot_visitor(factor_vm *parent_, slot_visitor<Fixup> *visitor_) :
274                 parent(parent_), visitor(visitor_) {}
275
276         /*
277         next  -> [entry_point]
278                  [size]
279                  [return address] -- x86 only, backend adds 1 to each spill location
280                  [spill area]
281                  ...
282         frame -> [entry_point]
283                  [size]
284         */
285         void operator()(stack_frame *frame)
286         {
287                 const code_block *compiled = visitor->fixup.translate_code(parent->frame_code(frame));
288                 gc_info *info = compiled->block_gc_info();
289
290                 u32 return_address = (cell)FRAME_RETURN_ADDRESS(frame,parent) - (cell)compiled->entry_point();
291                 int index = info->return_address_index(return_address);
292
293                 if(index != -1)
294                 {
295                         u8 *bitmap = info->gc_info_bitmap();
296                         cell base = info->spill_slot_base(index);
297                         cell *stack_pointer = (cell *)(parent->frame_successor(frame) + 1);
298
299                         for(cell spill_slot = 0; spill_slot < info->gc_root_count; spill_slot++)
300                         {
301                                 if(bitmap_p(bitmap,base + spill_slot))
302                                         visitor->visit_handle(&stack_pointer[spill_slot]);
303                         }
304                 }
305         }
306 };
307
308 template<typename Fixup>
309 void slot_visitor<Fixup>::visit_callstack_object(callstack *stack)
310 {
311         call_frame_slot_visitor<Fixup> call_frame_visitor(parent,this);
312         parent->iterate_callstack_object(stack,call_frame_visitor);
313 }
314
315 template<typename Fixup>
316 void slot_visitor<Fixup>::visit_callstack(context *ctx)
317 {
318         call_frame_slot_visitor<Fixup> call_frame_visitor(parent,this);
319         parent->iterate_callstack(ctx,call_frame_visitor);
320 }
321
322 template<typename Fixup>
323 void slot_visitor<Fixup>::visit_contexts()
324 {
325         std::set<context *>::const_iterator begin = parent->active_contexts.begin();
326         std::set<context *>::const_iterator end = parent->active_contexts.end();
327         while(begin != end)
328         {
329                 context *ctx = *begin;
330
331                 visit_stack_elements(ctx->datastack_seg,(cell *)ctx->datastack);
332                 visit_stack_elements(ctx->retainstack_seg,(cell *)ctx->retainstack);
333                 visit_object_array(ctx->context_objects,ctx->context_objects + context_object_count);
334                 visit_callstack(ctx);
335                 begin++;
336         }
337 }
338
339 template<typename Fixup>
340 struct literal_references_visitor {
341         slot_visitor<Fixup> *visitor;
342
343         explicit literal_references_visitor(slot_visitor<Fixup> *visitor_) : visitor(visitor_) {}
344
345         void operator()(instruction_operand op)
346         {
347                 if(op.rel_type() == RT_LITERAL)
348                         op.store_value(visitor->visit_pointer(op.load_value()));
349         }
350 };
351
352 template<typename Fixup>
353 void slot_visitor<Fixup>::visit_code_block_objects(code_block *compiled)
354 {
355         visit_handle(&compiled->owner);
356         visit_handle(&compiled->parameters);
357         visit_handle(&compiled->relocation);
358 }
359
360 template<typename Fixup>
361 void slot_visitor<Fixup>::visit_embedded_literals(code_block *compiled)
362 {
363         if(!parent->code->uninitialized_p(compiled))
364         {
365                 literal_references_visitor<Fixup> visitor(this);
366                 compiled->each_instruction_operand(visitor);
367         }
368 }
369
370 }