4 /* Code block visitors iterate over sets of code blocks, applying a functor to
5 each one. The functor returns a new code_block pointer, which may or may not
6 equal the old one. This is stored back to the original location.
8 This is used by GC's sweep and compact phases, and the implementation of the
9 modify-code-heap primitive.
11 Iteration is driven by visit_*() methods. Some of them define GC roots:
12 - visit_context_code_blocks()
13 - visit_callback_code_blocks() */
15 template<typename Fixup> struct code_block_visitor {
19 explicit code_block_visitor(factor_vm *parent_, Fixup fixup_) :
20 parent(parent_), fixup(fixup_) {}
22 code_block *visit_code_block(code_block *compiled);
23 void visit_object_code_block(object *obj);
24 void visit_embedded_code_pointers(code_block *compiled);
25 void visit_context_code_blocks();
26 void visit_uninitialized_code_blocks();
28 void visit_code_roots();
31 template<typename Fixup>
32 code_block *code_block_visitor<Fixup>::visit_code_block(code_block *compiled)
34 return fixup.fixup_code(compiled);
37 template<typename Fixup>
38 struct call_frame_code_block_visitor {
42 explicit call_frame_code_block_visitor(factor_vm *parent_, Fixup fixup_) :
43 parent(parent_), fixup(fixup_) {}
45 void operator()(stack_frame *frame)
47 cell offset = parent->frame_offset(frame);
48 code_block *compiled = fixup.fixup_code(parent->frame_code(frame));
49 frame->entry_point = compiled->entry_point();
50 parent->set_frame_offset(frame,offset);
54 template<typename Fixup>
55 void code_block_visitor<Fixup>::visit_object_code_block(object *obj)
61 word *w = (word *)obj;
63 w->entry_point = visit_code_block(w->code())->entry_point();
68 quotation *q = (quotation *)obj;
70 q->entry_point = visit_code_block(q->code())->entry_point();
75 callstack *stack = (callstack *)obj;
76 call_frame_code_block_visitor<Fixup> call_frame_visitor(parent,fixup);
77 parent->iterate_callstack_object(stack,call_frame_visitor);
83 template<typename Fixup>
84 struct embedded_code_pointers_visitor {
87 explicit embedded_code_pointers_visitor(Fixup fixup_) : fixup(fixup_) {}
89 void operator()(instruction_operand op)
91 relocation_type type = op.rel_type();
92 if(type == RT_ENTRY_POINT
93 || type == RT_ENTRY_POINT_PIC
94 || type == RT_ENTRY_POINT_PIC_TAIL)
95 op.store_code_block(fixup.fixup_code(op.load_code_block()));
99 template<typename Fixup>
100 void code_block_visitor<Fixup>::visit_embedded_code_pointers(code_block *compiled)
102 if(!parent->code->uninitialized_p(compiled))
104 embedded_code_pointers_visitor<Fixup> operand_visitor(fixup);
105 compiled->each_instruction_operand(operand_visitor);
109 template<typename Fixup>
110 void code_block_visitor<Fixup>::visit_context_code_blocks()
112 call_frame_code_block_visitor<Fixup> call_frame_visitor(parent,fixup);
113 parent->iterate_active_callstacks(call_frame_visitor);
116 template<typename Fixup>
117 void code_block_visitor<Fixup>::visit_uninitialized_code_blocks()
119 std::map<code_block *, cell> *uninitialized_blocks = &parent->code->uninitialized_blocks;
120 std::map<code_block *, cell>::const_iterator iter = uninitialized_blocks->begin();
121 std::map<code_block *, cell>::const_iterator end = uninitialized_blocks->end();
123 std::map<code_block *, cell> new_uninitialized_blocks;
124 for(; iter != end; iter++)
126 new_uninitialized_blocks.insert(std::make_pair(
127 fixup.fixup_code(iter->first),
131 parent->code->uninitialized_blocks = new_uninitialized_blocks;
134 template<typename Fixup>
135 void code_block_visitor<Fixup>::visit_code_roots()
137 visit_uninitialized_code_blocks();