]> gitweb.factorcode.org Git - factor.git/blob - vm/slot_visitor.hpp
xmode.rules: removing test no longer needed
[factor.git] / vm / slot_visitor.hpp
1 namespace factor {
2
3 // Size sans alignment.
4 template <typename Fixup>
5 cell object::base_size(Fixup fixup) const {
6   switch (type()) {
7     case ARRAY_TYPE:
8       return array_size((array*)this);
9     case BIGNUM_TYPE:
10       return array_size((bignum*)this);
11     case BYTE_ARRAY_TYPE:
12       return array_size((byte_array*)this);
13     case STRING_TYPE:
14       return string_size(string_capacity((string*)this));
15     case TUPLE_TYPE: {
16       tuple_layout* layout = (tuple_layout*)fixup.translate_data(
17           untag<object>(((tuple*)this)->layout));
18       return tuple_size(layout);
19     }
20     case QUOTATION_TYPE:
21       return sizeof(quotation);
22     case WORD_TYPE:
23       return sizeof(word);
24     case FLOAT_TYPE:
25       return sizeof(boxed_float);
26     case DLL_TYPE:
27       return sizeof(dll);
28     case ALIEN_TYPE:
29       return sizeof(alien);
30     case WRAPPER_TYPE:
31       return sizeof(wrapper);
32     case CALLSTACK_TYPE: {
33       cell callstack_length = untag_fixnum(((callstack*)this)->length);
34       return callstack_object_size(callstack_length);
35     }
36     default:
37       critical_error("Invalid header in base_size", (cell)this);
38       return 0;
39   }
40 }
41
42 // Size of the object pointed to by an untagged pointer
43 template <typename Fixup>
44 cell object::size(Fixup fixup) const {
45   if (free_p())
46     return ((free_heap_block*)this)->size();
47   return align(base_size(fixup), data_alignment);
48 }
49
50 inline cell object::size() const { return size(no_fixup()); }
51
52 // The number of slots (cells) in an object which should be scanned by
53 // the GC. The number can vary in arrays and tuples, in all other
54 // types the number is a constant.
55 template <typename Fixup>
56 inline cell object::slot_count(Fixup fixup) const {
57   if (free_p())
58     return 0;
59
60   cell t = type();
61   if (t == ARRAY_TYPE) {
62     // capacity + n slots
63     return 1 + array_capacity((array*)this);
64   } else if (t == TUPLE_TYPE) {
65     tuple_layout* layout = (tuple_layout*)fixup.translate_data(
66         untag<object>(((tuple*)this)->layout));
67     // layout + n slots
68     return 1 + tuple_capacity(layout);
69   } else {
70     switch (t) {
71       // these objects do not refer to other objects at all
72       case FLOAT_TYPE:
73       case BYTE_ARRAY_TYPE:
74       case BIGNUM_TYPE:
75       case CALLSTACK_TYPE: return 0;
76       case WORD_TYPE: return 8;
77       case ALIEN_TYPE: return 2;
78       case DLL_TYPE: return 1;
79       case QUOTATION_TYPE: return 3;
80       case STRING_TYPE: return 3;
81       case WRAPPER_TYPE: return 1;
82       default:
83         critical_error("Invalid header in slot_count", (cell)this);
84         return 0; // can't happen
85     }
86   }
87 }
88
89 inline cell object::slot_count() const {
90   return slot_count(no_fixup());
91 }
92
93 // Slot visitors iterate over the slots of an object, applying a functor to
94 // each one that is a non-immediate slot. The pointer is untagged first.
95 // The functor returns a new untagged object pointer. The return value may
96 // or may not equal the old one, however the new pointer receives the same
97 // tag before being stored back to the original location.
98
99 // Slots storing immediate values are left unchanged and the visitor does
100 // inspect them.
101
102 // This is used by GC's copying, sweep and compact phases, and the
103 // implementation of the become primitive.
104
105 // Iteration is driven by visit_*() methods. Only one of them define GC
106 // roots:
107 //  - visit_all_roots()
108
109 // Code block visitors iterate over sets of code blocks, applying a functor
110 // to each one. The functor returns a new code_block pointer, which may or
111 // may not equal the old one. This is stored back to the original location.
112
113 // This is used by GC's sweep and compact phases, and the implementation of
114 // the modify-code-heap primitive.
115
116 // Iteration is driven by visit_*() methods. Some of them define GC roots:
117 //  - visit_context_code_blocks()
118 //  - visit_callback_code_blocks()
119
120 template <typename Fixup> struct slot_visitor {
121   factor_vm* parent;
122   Fixup fixup;
123   cell cards_scanned;
124   cell decks_scanned;
125
126   slot_visitor<Fixup>(factor_vm* parent, Fixup fixup)
127   : parent(parent),
128     fixup(fixup),
129     cards_scanned(0),
130     decks_scanned(0) {}
131
132   cell visit_pointer(cell pointer);
133   void visit_handle(cell* handle);
134   void visit_object_array(cell* start, cell* end);
135   void visit_partial_objects(cell start, cell card_start, cell card_end);
136   void visit_slots(object* ptr);
137   void visit_stack_elements(segment* region, cell* top);
138   void visit_all_roots();
139   void visit_callstack_object(callstack* stack);
140   void visit_callstack(context* ctx);
141   void visit_context(context *ctx);
142   void visit_object_code_block(object* obj);
143   void visit_context_code_blocks();
144   void visit_uninitialized_code_blocks();
145   void visit_object(object* obj);
146   void visit_mark_stack(std::vector<cell>* mark_stack);
147
148
149   template <typename SourceGeneration>
150   cell visit_card(SourceGeneration* gen, cell index, cell start);
151   template <typename SourceGeneration>
152   void visit_cards(SourceGeneration* gen, card mask, card unmask);
153
154
155   template <typename TargetGeneration>
156   void cheneys_algorithm(TargetGeneration* gen, cell scan);
157
158   // Visits the data pointers in code blocks in the remembered set.
159   void visit_code_heap_roots(std::set<code_block*>* remembered_set);
160
161   // Visits pointers embedded in instructions in code blocks.
162   void visit_instruction_operands(code_block* block, cell rel_base);
163   void visit_embedded_code_pointers(code_block* compiled);
164   void visit_embedded_literals(code_block* compiled);
165
166   // Visits data pointers in code blocks.
167   void visit_code_block_objects(code_block* compiled);
168 };
169
170 template <typename Fixup>
171 cell slot_visitor<Fixup>::visit_pointer(cell pointer) {
172   object* untagged = fixup.fixup_data(untag<object>(pointer));
173   return RETAG(untagged, TAG(pointer));
174 }
175
176 template <typename Fixup> void slot_visitor<Fixup>::visit_handle(cell* handle) {
177   if (!immediate_p(*handle)) {
178     *handle = visit_pointer(*handle);
179   }
180 }
181
182 template <typename Fixup>
183 void slot_visitor<Fixup>::visit_object_array(cell* start, cell* end) {
184   while (start < end)
185     visit_handle(start++);
186 }
187
188 template <typename Fixup> void slot_visitor<Fixup>::visit_slots(object* obj) {
189   if (obj->type() == CALLSTACK_TYPE)
190     visit_callstack_object((callstack*)obj);
191   else {
192     cell* start = (cell*)obj + 1;
193     cell* end = start + obj->slot_count(fixup);
194     visit_object_array(start, end);
195   }
196 }
197
198 template <typename Fixup>
199 void slot_visitor<Fixup>::visit_stack_elements(segment* region, cell* top) {
200   visit_object_array((cell*)region->start, top + 1);
201 }
202
203 template <typename Fixup> void slot_visitor<Fixup>::visit_all_roots() {
204   FACTOR_FOR_EACH(parent->data_roots) {
205     visit_handle(*iter);
206   }
207
208   auto callback_slot_visitor = [&](code_block* stub, cell size) {
209           (void)size;
210     visit_handle(&stub->owner);
211   };
212   parent->callbacks->allocator->iterate(callback_slot_visitor, no_fixup());
213
214   FACTOR_FOR_EACH(parent->code->uninitialized_blocks) {
215     iter->second = visit_pointer(iter->second);
216   }
217
218   FACTOR_FOR_EACH(parent->samples) {
219     visit_handle(&iter->thread);
220   }
221
222   visit_object_array(parent->special_objects,
223                      parent->special_objects + special_object_count);
224
225   FACTOR_FOR_EACH(parent->active_contexts) {
226     visit_context(*iter);
227   }
228 }
229
230 // primitive_minor_gc() is invoked by inline GC checks, and it needs to
231 // visit spill slots which references objects in the heap.
232
233 // So for each call frame:
234 //  - trace roots in spill slots
235
236 template <typename Fixup> struct call_frame_slot_visitor {
237   slot_visitor<Fixup>* visitor;
238
239   call_frame_slot_visitor(slot_visitor<Fixup>* visitor)
240       : visitor(visitor) {}
241
242   // frame top -> [return address]
243   //              [spill area]
244   //              ...
245   //              [entry_point]
246   //              [size]
247
248   void operator()(cell frame_top, cell size, code_block* owner, cell addr) {
249           (void)size;
250     cell return_address = owner->offset(addr);
251
252     code_block* compiled =
253         Fixup::translated_code_block_map ? owner
254                                          : visitor->fixup.translate_code(owner);
255     gc_info* info = compiled->block_gc_info();
256
257     FACTOR_ASSERT(return_address < compiled->size());
258     cell callsite = info->return_address_index(return_address);
259     if (callsite == (cell)-1)
260       return;
261
262 #ifdef DEBUG_GC_MAPS
263     FACTOR_PRINT("call frame code block " << compiled << " with offset "
264                  << return_address);
265 #endif
266     cell* stack_pointer = (cell*)frame_top;
267     uint8_t* bitmap = info->gc_info_bitmap();
268
269     // Subtract old value of base pointer from every derived pointer.
270     for (cell spill_slot = 0; spill_slot < info->derived_root_count;
271          spill_slot++) {
272       uint32_t base_pointer = info->lookup_base_pointer(callsite, spill_slot);
273       if (base_pointer != (uint32_t)-1) {
274 #ifdef DEBUG_GC_MAPS
275         FACTOR_PRINT("visiting derived root " << spill_slot
276                      << " with base pointer " << base_pointer);
277 #endif
278         stack_pointer[spill_slot] -= stack_pointer[base_pointer];
279       }
280     }
281
282     // Update all GC roots, including base pointers.
283     cell callsite_gc_roots = info->callsite_gc_roots(callsite);
284
285     for (cell spill_slot = 0; spill_slot < info->gc_root_count; spill_slot++) {
286       if (bitmap_p(bitmap, callsite_gc_roots + spill_slot)) {
287         #ifdef DEBUG_GC_MAPS
288         FACTOR_PRINT("visiting GC root " << spill_slot);
289         #endif
290         visitor->visit_handle(stack_pointer + spill_slot);
291       }
292     }
293
294     // Add the base pointers to obtain new derived pointer values.
295     for (cell spill_slot = 0; spill_slot < info->derived_root_count;
296          spill_slot++) {
297       uint32_t base_pointer = info->lookup_base_pointer(callsite, spill_slot);
298       if (base_pointer != (uint32_t)-1)
299         stack_pointer[spill_slot] += stack_pointer[base_pointer];
300     }
301   }
302 };
303
304 template <typename Fixup>
305 void slot_visitor<Fixup>::visit_callstack_object(callstack* stack) {
306   call_frame_slot_visitor<Fixup> call_frame_visitor(this);
307   parent->iterate_callstack_object(stack, call_frame_visitor, fixup);
308 }
309
310 template <typename Fixup>
311 void slot_visitor<Fixup>::visit_callstack(context* ctx) {
312   call_frame_slot_visitor<Fixup> call_frame_visitor(this);
313   parent->iterate_callstack(ctx, call_frame_visitor, fixup);
314 }
315
316 template <typename Fixup>
317 void slot_visitor<Fixup>::visit_context(context* ctx) {
318   visit_callstack(ctx);
319
320   cell ds_ptr = ctx->datastack;
321   cell rs_ptr = ctx->retainstack;
322   segment* ds_seg = ctx->datastack_seg;
323   segment* rs_seg = ctx->retainstack_seg;
324   visit_stack_elements(ds_seg, (cell*)ds_ptr);
325   visit_stack_elements(rs_seg, (cell*)rs_ptr);
326   visit_object_array(ctx->context_objects,
327                      ctx->context_objects + context_object_count);
328
329   // Clear out the space not visited with a known pattern. That makes
330   // it easier to see if uninitialized reads are made.
331   ctx->fill_stack_seg(ds_ptr, ds_seg, 0xbaadbadd);
332   ctx->fill_stack_seg(rs_ptr, rs_seg, 0xdaabdabb);
333 }
334
335 template <typename Fixup>
336 void slot_visitor<Fixup>::visit_code_block_objects(code_block* compiled) {
337   visit_handle(&compiled->owner);
338   visit_handle(&compiled->parameters);
339   visit_handle(&compiled->relocation);
340 }
341
342 template <typename Fixup>
343 void slot_visitor<Fixup>::visit_embedded_literals(code_block* compiled) {
344   if (parent->code->uninitialized_p(compiled))
345     return;
346
347   auto update_literal_refs = [&](instruction_operand op) {
348     if (op.rel.type() == RT_LITERAL) {
349       cell value = op.load_value(op.pointer);
350       if (!immediate_p(value)) {
351         op.store_value(visit_pointer(value));
352       }
353     }
354   };
355   compiled->each_instruction_operand(update_literal_refs);
356 }
357
358 template <typename Fixup> struct call_frame_code_block_visitor {
359   Fixup fixup;
360
361   call_frame_code_block_visitor(Fixup fixup) : fixup(fixup) {}
362
363   void operator()(cell frame_top, cell size, code_block* owner, cell addr) {
364     (void)size;
365           code_block* compiled =
366         Fixup::translated_code_block_map ? owner : fixup.fixup_code(owner);
367     cell fixed_addr = compiled->address_for_offset(owner->offset(addr));
368
369     *(cell*)frame_top = fixed_addr;
370   }
371 };
372
373 template <typename Fixup>
374 void slot_visitor<Fixup>::visit_object_code_block(object* obj) {
375   switch (obj->type()) {
376     case WORD_TYPE: {
377       word* w = (word*)obj;
378       if (w->entry_point)
379         w->entry_point = fixup.fixup_code(w->code())->entry_point();
380       break;
381     }
382     case QUOTATION_TYPE: {
383       quotation* q = (quotation*)obj;
384       if (q->entry_point)
385         q->entry_point = fixup.fixup_code(q->code())->entry_point();
386       break;
387     }
388     case CALLSTACK_TYPE: {
389       callstack* stack = (callstack*)obj;
390       call_frame_code_block_visitor<Fixup> call_frame_visitor(fixup);
391       parent->iterate_callstack_object(stack, call_frame_visitor, fixup);
392       break;
393     }
394   }
395 }
396
397 template <typename Fixup>
398 void slot_visitor<Fixup>::visit_context_code_blocks() {
399   call_frame_code_block_visitor<Fixup> call_frame_visitor(fixup);
400   FACTOR_FOR_EACH(parent->active_contexts) {
401     parent->iterate_callstack(*iter, call_frame_visitor, fixup);
402   }
403 }
404
405 template <typename Fixup>
406 void slot_visitor<Fixup>::visit_uninitialized_code_blocks() {
407   std::map<code_block*, cell> new_uninitialized_blocks;
408   FACTOR_FOR_EACH(parent->code->uninitialized_blocks) {
409     new_uninitialized_blocks.insert(
410         std::make_pair(fixup.fixup_code(iter->first), iter->second));
411   }
412   parent->code->uninitialized_blocks = new_uninitialized_blocks;
413 }
414
415 template <typename Fixup>
416 void slot_visitor<Fixup>::visit_embedded_code_pointers(code_block* compiled) {
417   if (parent->code->uninitialized_p(compiled))
418     return;
419   auto update_code_block_refs = [&](instruction_operand op){
420     relocation_type type = op.rel.type();
421     if (type == RT_ENTRY_POINT ||
422         type == RT_ENTRY_POINT_PIC ||
423         type == RT_ENTRY_POINT_PIC_TAIL) {
424       code_block* block = fixup.fixup_code(op.load_code_block());
425       op.store_value(block->entry_point());
426     }
427   };
428   compiled->each_instruction_operand(update_code_block_refs);
429 }
430
431 template <typename Fixup>
432 void slot_visitor<Fixup>::visit_object(object *ptr) {
433   visit_slots(ptr);
434   if (ptr->type() == ALIEN_TYPE)
435     ((alien*)ptr)->update_address();
436 }
437
438 // Pops items from the mark stack and visits them until the stack is
439 // empty. Used when doing a full collection and when collecting to
440 // tenured space.
441 template <typename Fixup>
442 void slot_visitor<Fixup>::visit_mark_stack(std::vector<cell>* mark_stack) {
443   while (!mark_stack->empty()) {
444     cell ptr = mark_stack->back();
445     mark_stack->pop_back();
446
447     if (ptr & 1) {
448       code_block* compiled = (code_block*)(ptr - 1);
449       visit_code_block_objects(compiled);
450       visit_embedded_literals(compiled);
451       visit_embedded_code_pointers(compiled);
452     } else {
453       object* obj = (object*)ptr;
454       visit_object(obj);
455       visit_object_code_block(obj);
456     }
457   }
458 }
459
460 // Visits the instruction operands in a code block. If the operand is
461 // a pointer to a code block or data object, then the fixup is applied
462 // to it. Otherwise, if it is an external addess, that address is
463 // recomputed. If it is an untagged number literal (RT_UNTAGGED) or an
464 // immediate value, then nothing is done with it.
465 template <typename Fixup>
466 void slot_visitor<Fixup>::visit_instruction_operands(code_block* block,
467                                                      cell rel_base) {
468   auto visit_func = [&](instruction_operand op){
469     cell old_offset = rel_base + op.rel.offset();
470     cell old_value = op.load_value(old_offset);
471     switch (op.rel.type()) {
472       case RT_LITERAL: {
473         if (!immediate_p(old_value)) {
474           op.store_value(visit_pointer(old_value));
475         }
476         break;
477       }
478       case RT_ENTRY_POINT:
479       case RT_ENTRY_POINT_PIC:
480       case RT_ENTRY_POINT_PIC_TAIL:
481       case RT_HERE: {
482         cell offset = TAG(old_value);
483         code_block* compiled = (code_block*)UNTAG(old_value);
484         op.store_value(RETAG(fixup.fixup_code(compiled), offset));
485         break;
486       }
487       case RT_UNTAGGED:
488         break;
489       default:
490         op.store_value(parent->compute_external_address(op));
491         break;
492     }
493   };
494   if (parent->code->uninitialized_p(block))
495     return;
496   block->each_instruction_operand(visit_func);
497 }
498
499 template <typename Fixup>
500 void slot_visitor<Fixup>::visit_partial_objects(cell start,
501                                                 cell card_start,
502                                                 cell card_end) {
503   cell *scan_start = (cell*)start + 1;
504   cell *scan_end = scan_start + ((object*)start)->slot_count();
505
506   scan_start = std::max(scan_start, (cell*)card_start);
507   scan_end = std::min(scan_end, (cell*)card_end);
508
509   visit_object_array(scan_start, scan_end);
510 }
511
512 template <typename Fixup>
513 template <typename SourceGeneration>
514 cell slot_visitor<Fixup>::visit_card(SourceGeneration* gen,
515                                      cell index, cell start) {
516   cell heap_base = parent->data->start;
517   cell start_addr = heap_base + index * card_size;
518   cell end_addr = start_addr + card_size;
519
520   // Forward to the next object whose address is in the card.
521   if (!start || (start + ((object*)start)->size()) < start_addr) {
522     // Optimization because finding the objects in a memory range is
523     // expensive. It helps a lot when tracing consecutive cards.
524     cell gen_start_card = (gen->start - heap_base) / card_size;
525     start = gen->starts
526         .find_object_containing_card(index - gen_start_card);
527   }
528
529   while (start && start < end_addr) {
530     visit_partial_objects(start, start_addr, end_addr);
531     if ((start + ((object*)start)->size()) >= end_addr) {
532       // The object can overlap the card boundary, then the
533       // remainder of it will be handled in the next card
534       // tracing if that card is marked.
535       break;
536     }
537     start = gen->next_object_after(start);
538   }
539   return start;
540 }
541
542 template <typename Fixup>
543 template <typename SourceGeneration>
544 void slot_visitor<Fixup>::visit_cards(SourceGeneration* gen,
545                                       card mask, card unmask) {
546   card_deck* decks = parent->data->decks;
547   card_deck* cards = parent->data->cards;
548   cell heap_base = parent->data->start;
549
550   cell first_deck = (gen->start - heap_base) / deck_size;
551   cell last_deck = (gen->end - heap_base) / deck_size;
552
553   // Address of last traced object.
554   cell start = 0;
555   for (cell di = first_deck; di < last_deck; di++) {
556     if (decks[di] & mask) {
557       decks[di] &= ~unmask;
558       decks_scanned++;
559
560       cell first_card = cards_per_deck * di;
561       cell last_card = first_card + cards_per_deck;
562
563       for (cell ci = first_card; ci < last_card; ci++) {
564         if (cards[ci] & mask) {
565           cards[ci] &= ~unmask;
566           cards_scanned++;
567
568           start = visit_card(gen, ci, start);
569           if (!start) {
570             // At end of generation, no need to scan more cards.
571             return;
572           }
573         }
574       }
575     }
576   }
577 }
578
579 template <typename Fixup>
580 void slot_visitor<Fixup>::visit_code_heap_roots(std::set<code_block*>* remembered_set) {
581   FACTOR_FOR_EACH(*remembered_set) {
582     code_block* compiled = *iter;
583     visit_code_block_objects(compiled);
584     visit_embedded_literals(compiled);
585     compiled->flush_icache();
586   }
587 }
588
589 template <typename Fixup>
590 template <typename TargetGeneration>
591 void slot_visitor<Fixup>::cheneys_algorithm(TargetGeneration* gen, cell scan) {
592   while (scan && scan < gen->here) {
593     visit_object((object*)scan);
594     scan = gen->next_object_after(scan);
595   }
596 }
597
598 }