3 // Size sans alignment.
4 template <typename Fixup>
5 cell object::base_size(Fixup fixup) const {
8 return array_size((array*)this);
10 return array_size((bignum*)this);
12 return array_size((byte_array*)this);
14 return string_size(string_capacity((string*)this));
16 tuple_layout* layout = (tuple_layout*)fixup.translate_data(
17 untag<object>(((tuple*)this)->layout));
18 return tuple_size(layout);
21 return sizeof(quotation);
25 return sizeof(boxed_float);
31 return sizeof(wrapper);
32 case CALLSTACK_TYPE: {
33 cell callstack_length = untag_fixnum(((callstack*)this)->length);
34 return callstack_object_size(callstack_length);
37 critical_error("Invalid header in base_size", (cell)this);
42 // Size of the object pointed to by an untagged pointer
43 template <typename Fixup>
44 cell object::size(Fixup fixup) const {
46 return ((free_heap_block*)this)->size();
47 return align(base_size(fixup), data_alignment);
50 inline cell object::size() const { return size(no_fixup()); }
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 {
61 if (t == ARRAY_TYPE) {
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));
68 return 1 + tuple_capacity(layout);
71 // these objects do not refer to other objects at all
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;
83 critical_error("Invalid header in slot_count", (cell)this);
84 return 0; // can't happen
89 inline cell object::slot_count() const {
90 return slot_count(no_fixup());
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.
99 // Slots storing immediate values are left unchanged and the visitor does
102 // This is used by GC's copying, sweep and compact phases, and the
103 // implementation of the become primitive.
105 // Iteration is driven by visit_*() methods. Only one of them define GC
107 // - visit_all_roots()
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.
113 // This is used by GC's sweep and compact phases, and the implementation of
114 // the modify-code-heap primitive.
116 // Iteration is driven by visit_*() methods. Some of them define GC roots:
117 // - visit_context_code_blocks()
118 // - visit_callback_code_blocks()
120 template <typename Fixup> struct slot_visitor {
126 slot_visitor<Fixup>(factor_vm* parent, Fixup fixup)
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);
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);
155 template <typename TargetGeneration>
156 void cheneys_algorithm(TargetGeneration* gen, cell scan);
158 // Visits the data pointers in code blocks in the remembered set.
159 void visit_code_heap_roots(std::set<code_block*>* remembered_set);
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);
166 // Visits data pointers in code blocks.
167 void visit_code_block_objects(code_block* compiled);
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));
176 template <typename Fixup> void slot_visitor<Fixup>::visit_handle(cell* handle) {
177 if (!immediate_p(*handle)) {
178 *handle = visit_pointer(*handle);
182 template <typename Fixup>
183 void slot_visitor<Fixup>::visit_object_array(cell* start, cell* end) {
185 visit_handle(start++);
188 template <typename Fixup> void slot_visitor<Fixup>::visit_slots(object* obj) {
189 if (obj->type() == CALLSTACK_TYPE)
190 visit_callstack_object((callstack*)obj);
192 cell* start = (cell*)obj + 1;
193 cell* end = start + obj->slot_count(fixup);
194 visit_object_array(start, end);
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);
203 template <typename Fixup> void slot_visitor<Fixup>::visit_all_roots() {
204 FACTOR_FOR_EACH(parent->data_roots) {
208 auto callback_slot_visitor = [&](code_block* stub, cell size) {
209 visit_handle(&stub->owner);
211 parent->callbacks->allocator->iterate(callback_slot_visitor, no_fixup());
213 FACTOR_FOR_EACH(parent->code->uninitialized_blocks) {
214 iter->second = visit_pointer(iter->second);
217 FACTOR_FOR_EACH(parent->samples) {
218 visit_handle(&iter->thread);
221 visit_object_array(parent->special_objects,
222 parent->special_objects + special_object_count);
224 FACTOR_FOR_EACH(parent->active_contexts) {
225 visit_context(*iter);
229 // primitive_minor_gc() is invoked by inline GC checks, and it needs to
230 // visit spill slots which references objects in the heap.
232 // So for each call frame:
233 // - trace roots in spill slots
235 template <typename Fixup> struct call_frame_slot_visitor {
236 slot_visitor<Fixup>* visitor;
238 call_frame_slot_visitor(slot_visitor<Fixup>* visitor)
239 : visitor(visitor) {}
241 // frame top -> [return address]
247 void operator()(cell frame_top, cell size, code_block* owner, cell addr) {
248 cell return_address = owner->offset(addr);
250 code_block* compiled =
251 Fixup::translated_code_block_map ? owner
252 : visitor->fixup.translate_code(owner);
253 gc_info* info = compiled->block_gc_info();
255 FACTOR_ASSERT(return_address < compiled->size());
256 cell callsite = info->return_address_index(return_address);
257 if (callsite == (cell)-1)
261 FACTOR_PRINT("call frame code block " << compiled << " with offset "
264 cell* stack_pointer = (cell*)frame_top;
265 uint8_t* bitmap = info->gc_info_bitmap();
267 // Subtract old value of base pointer from every derived pointer.
268 for (cell spill_slot = 0; spill_slot < info->derived_root_count;
270 uint32_t base_pointer = info->lookup_base_pointer(callsite, spill_slot);
271 if (base_pointer != (uint32_t)-1) {
273 FACTOR_PRINT("visiting derived root " << spill_slot
274 << " with base pointer " << base_pointer);
276 stack_pointer[spill_slot] -= stack_pointer[base_pointer];
280 // Update all GC roots, including base pointers.
281 cell callsite_gc_roots = info->callsite_gc_roots(callsite);
283 for (cell spill_slot = 0; spill_slot < info->gc_root_count; spill_slot++) {
284 if (bitmap_p(bitmap, callsite_gc_roots + spill_slot)) {
286 FACTOR_PRINT("visiting GC root " << spill_slot);
288 visitor->visit_handle(stack_pointer + spill_slot);
292 // Add the base pointers to obtain new derived pointer values.
293 for (cell spill_slot = 0; spill_slot < info->derived_root_count;
295 uint32_t base_pointer = info->lookup_base_pointer(callsite, spill_slot);
296 if (base_pointer != (uint32_t)-1)
297 stack_pointer[spill_slot] += stack_pointer[base_pointer];
302 template <typename Fixup>
303 void slot_visitor<Fixup>::visit_callstack_object(callstack* stack) {
304 call_frame_slot_visitor<Fixup> call_frame_visitor(this);
305 parent->iterate_callstack_object(stack, call_frame_visitor, fixup);
308 template <typename Fixup>
309 void slot_visitor<Fixup>::visit_callstack(context* ctx) {
310 call_frame_slot_visitor<Fixup> call_frame_visitor(this);
311 parent->iterate_callstack(ctx, call_frame_visitor, fixup);
314 template <typename Fixup>
315 void slot_visitor<Fixup>::visit_context(context* ctx) {
316 visit_callstack(ctx);
318 cell ds_ptr = ctx->datastack;
319 cell rs_ptr = ctx->retainstack;
320 segment* ds_seg = ctx->datastack_seg;
321 segment* rs_seg = ctx->retainstack_seg;
322 visit_stack_elements(ds_seg, (cell*)ds_ptr);
323 visit_stack_elements(rs_seg, (cell*)rs_ptr);
324 visit_object_array(ctx->context_objects,
325 ctx->context_objects + context_object_count);
327 // Clear out the space not visited with a known pattern. That makes
328 // it easier to see if uninitialized reads are made.
329 ctx->fill_stack_seg(ds_ptr, ds_seg, 0xbaadbadd);
330 ctx->fill_stack_seg(rs_ptr, rs_seg, 0xdaabdabb);
333 template <typename Fixup>
334 void slot_visitor<Fixup>::visit_code_block_objects(code_block* compiled) {
335 visit_handle(&compiled->owner);
336 visit_handle(&compiled->parameters);
337 visit_handle(&compiled->relocation);
340 template <typename Fixup>
341 void slot_visitor<Fixup>::visit_embedded_literals(code_block* compiled) {
342 if (parent->code->uninitialized_p(compiled))
345 auto update_literal_refs = [&](instruction_operand op) {
346 if (op.rel.type() == RT_LITERAL) {
347 cell value = op.load_value(op.pointer);
348 if (!immediate_p(value)) {
349 op.store_value(visit_pointer(value));
353 compiled->each_instruction_operand(update_literal_refs);
356 template <typename Fixup> struct call_frame_code_block_visitor {
359 call_frame_code_block_visitor(Fixup fixup) : fixup(fixup) {}
361 void operator()(cell frame_top, cell size, code_block* owner, cell addr) {
362 code_block* compiled =
363 Fixup::translated_code_block_map ? owner : fixup.fixup_code(owner);
364 cell fixed_addr = compiled->address_for_offset(owner->offset(addr));
366 *(cell*)frame_top = fixed_addr;
370 template <typename Fixup>
371 void slot_visitor<Fixup>::visit_object_code_block(object* obj) {
372 switch (obj->type()) {
374 word* w = (word*)obj;
376 w->entry_point = fixup.fixup_code(w->code())->entry_point();
379 case QUOTATION_TYPE: {
380 quotation* q = (quotation*)obj;
382 q->entry_point = fixup.fixup_code(q->code())->entry_point();
385 case CALLSTACK_TYPE: {
386 callstack* stack = (callstack*)obj;
387 call_frame_code_block_visitor<Fixup> call_frame_visitor(fixup);
388 parent->iterate_callstack_object(stack, call_frame_visitor, fixup);
394 template <typename Fixup>
395 void slot_visitor<Fixup>::visit_context_code_blocks() {
396 call_frame_code_block_visitor<Fixup> call_frame_visitor(fixup);
397 FACTOR_FOR_EACH(parent->active_contexts) {
398 parent->iterate_callstack(*iter, call_frame_visitor, fixup);
402 template <typename Fixup>
403 void slot_visitor<Fixup>::visit_uninitialized_code_blocks() {
404 std::map<code_block*, cell> new_uninitialized_blocks;
405 FACTOR_FOR_EACH(parent->code->uninitialized_blocks) {
406 new_uninitialized_blocks.insert(
407 std::make_pair(fixup.fixup_code(iter->first), iter->second));
409 parent->code->uninitialized_blocks = new_uninitialized_blocks;
412 template <typename Fixup>
413 void slot_visitor<Fixup>::visit_embedded_code_pointers(code_block* compiled) {
414 if (parent->code->uninitialized_p(compiled))
416 auto update_code_block_refs = [&](instruction_operand op){
417 relocation_type type = op.rel.type();
418 if (type == RT_ENTRY_POINT ||
419 type == RT_ENTRY_POINT_PIC ||
420 type == RT_ENTRY_POINT_PIC_TAIL) {
421 code_block* block = fixup.fixup_code(op.load_code_block());
422 op.store_value(block->entry_point());
425 compiled->each_instruction_operand(update_code_block_refs);
428 template <typename Fixup>
429 void slot_visitor<Fixup>::visit_object(object *ptr) {
431 if (ptr->type() == ALIEN_TYPE)
432 ((alien*)ptr)->update_address();
435 // Pops items from the mark stack and visits them until the stack is
436 // empty. Used when doing a full collection and when collecting to
438 template <typename Fixup>
439 void slot_visitor<Fixup>::visit_mark_stack(std::vector<cell>* mark_stack) {
440 while (!mark_stack->empty()) {
441 cell ptr = mark_stack->back();
442 mark_stack->pop_back();
445 code_block* compiled = (code_block*)(ptr - 1);
446 visit_code_block_objects(compiled);
447 visit_embedded_literals(compiled);
448 visit_embedded_code_pointers(compiled);
450 object* obj = (object*)ptr;
452 visit_object_code_block(obj);
457 // Visits the instruction operands in a code block. If the operand is
458 // a pointer to a code block or data object, then the fixup is applied
459 // to it. Otherwise, if it is an external addess, that address is
460 // recomputed. If it is an untagged number literal (RT_UNTAGGED) or an
461 // immediate value, then nothing is done with it.
462 template <typename Fixup>
463 void slot_visitor<Fixup>::visit_instruction_operands(code_block* block,
465 auto visit_func = [&](instruction_operand op){
466 cell old_offset = rel_base + op.rel.offset();
467 cell old_value = op.load_value(old_offset);
468 switch (op.rel.type()) {
470 if (!immediate_p(old_value)) {
471 op.store_value(visit_pointer(old_value));
476 case RT_ENTRY_POINT_PIC:
477 case RT_ENTRY_POINT_PIC_TAIL:
479 cell offset = TAG(old_value);
480 code_block* compiled = (code_block*)UNTAG(old_value);
481 op.store_value(RETAG(fixup.fixup_code(compiled), offset));
487 op.store_value(parent->compute_external_address(op));
491 if (parent->code->uninitialized_p(block))
493 block->each_instruction_operand(visit_func);
496 template <typename Fixup>
497 void slot_visitor<Fixup>::visit_partial_objects(cell start,
500 cell *scan_start = (cell*)start + 1;
501 cell *scan_end = scan_start + ((object*)start)->slot_count();
503 scan_start = std::max(scan_start, (cell*)card_start);
504 scan_end = std::min(scan_end, (cell*)card_end);
506 visit_object_array(scan_start, scan_end);
509 template <typename Fixup>
510 template <typename SourceGeneration>
511 cell slot_visitor<Fixup>::visit_card(SourceGeneration* gen,
512 cell index, cell start) {
513 cell heap_base = parent->data->start;
514 cell start_addr = heap_base + index * card_size;
515 cell end_addr = start_addr + card_size;
517 // Forward to the next object whose address is in the card.
518 if (!start || (start + ((object*)start)->size()) < start_addr) {
519 // Optimization because finding the objects in a memory range is
520 // expensive. It helps a lot when tracing consecutive cards.
521 cell gen_start_card = (gen->start - heap_base) / card_size;
523 .find_object_containing_card(index - gen_start_card);
526 while (start && start < end_addr) {
527 visit_partial_objects(start, start_addr, end_addr);
528 if ((start + ((object*)start)->size()) >= end_addr) {
529 // The object can overlap the card boundary, then the
530 // remainder of it will be handled in the next card
531 // tracing if that card is marked.
534 start = gen->next_object_after(start);
539 template <typename Fixup>
540 template <typename SourceGeneration>
541 void slot_visitor<Fixup>::visit_cards(SourceGeneration* gen,
542 card mask, card unmask) {
543 card_deck* decks = parent->data->decks;
544 card_deck* cards = parent->data->cards;
545 cell heap_base = parent->data->start;
547 cell first_deck = (gen->start - heap_base) / deck_size;
548 cell last_deck = (gen->end - heap_base) / deck_size;
550 // Address of last traced object.
552 for (cell di = first_deck; di < last_deck; di++) {
553 if (decks[di] & mask) {
554 decks[di] &= ~unmask;
557 cell first_card = cards_per_deck * di;
558 cell last_card = first_card + cards_per_deck;
560 for (cell ci = first_card; ci < last_card; ci++) {
561 if (cards[ci] & mask) {
562 cards[ci] &= ~unmask;
565 start = visit_card(gen, ci, start);
567 // At end of generation, no need to scan more cards.
576 template <typename Fixup>
577 void slot_visitor<Fixup>::visit_code_heap_roots(std::set<code_block*>* remembered_set) {
578 FACTOR_FOR_EACH(*remembered_set) {
579 code_block* compiled = *iter;
580 visit_code_block_objects(compiled);
581 visit_embedded_literals(compiled);
582 compiled->flush_icache();
586 template <typename Fixup>
587 template <typename TargetGeneration>
588 void slot_visitor<Fixup>::cheneys_algorithm(TargetGeneration* gen, cell scan) {
589 while (scan && scan < gen->here) {
590 visit_object((object*)scan);
591 scan = gen->next_object_after(scan);