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 Visitor> struct code_block_visitor {
19 explicit code_block_visitor(factor_vm *parent_, Visitor visitor_) :
20 parent(parent_), visitor(visitor_) {}
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();
29 template<typename Visitor>
30 code_block *code_block_visitor<Visitor>::visit_code_block(code_block *compiled)
32 return visitor(compiled);
35 template<typename Visitor>
36 struct call_frame_code_block_visitor {
40 explicit call_frame_code_block_visitor(factor_vm *parent_, Visitor visitor_) :
41 parent(parent_), visitor(visitor_) {}
43 void operator()(stack_frame *frame)
45 cell offset = (cell)FRAME_RETURN_ADDRESS(frame,parent) - (cell)frame->entry_point;
47 code_block *new_block = visitor(parent->frame_code(frame));
48 frame->entry_point = new_block->entry_point();
50 FRAME_RETURN_ADDRESS(frame,parent) = (void *)((cell)frame->entry_point + offset);
54 template<typename Visitor>
55 void code_block_visitor<Visitor>::visit_object_code_block(object *obj)
61 word *w = (word *)obj;
63 w->code = visitor(w->code);
65 w->profiling = visitor(w->profiling);
67 parent->update_word_entry_point(w);
72 quotation *q = (quotation *)obj;
74 parent->set_quot_entry_point(q,visitor(q->code));
79 callstack *stack = (callstack *)obj;
80 call_frame_code_block_visitor<Visitor> call_frame_visitor(parent,visitor);
81 parent->iterate_callstack_object(stack,call_frame_visitor);
87 template<typename Visitor>
88 struct embedded_code_pointers_visitor {
91 explicit embedded_code_pointers_visitor(Visitor visitor_) : visitor(visitor_) {}
93 void operator()(instruction_operand op)
95 relocation_type type = op.rel_type();
96 if(type == RT_ENTRY_POINT
97 || type == RT_ENTRY_POINT_PIC
98 || type == RT_ENTRY_POINT_PIC_TAIL)
99 op.store_code_block(visitor(op.load_code_block()));
103 template<typename Visitor>
104 void code_block_visitor<Visitor>::visit_embedded_code_pointers(code_block *compiled)
106 if(!parent->code->uninitialized_p(compiled))
108 embedded_code_pointers_visitor<Visitor> visitor(this->visitor);
109 compiled->each_instruction_operand(visitor);
113 template<typename Visitor>
114 void code_block_visitor<Visitor>::visit_context_code_blocks()
116 call_frame_code_block_visitor<Visitor> call_frame_visitor(parent,visitor);
117 parent->iterate_active_callstacks(call_frame_visitor);
120 template<typename Visitor>
121 void code_block_visitor<Visitor>::visit_uninitialized_code_blocks()
123 std::map<code_block *, cell> *uninitialized_blocks = &parent->code->uninitialized_blocks;
124 std::map<code_block *, cell>::const_iterator iter = uninitialized_blocks->begin();
125 std::map<code_block *, cell>::const_iterator end = uninitialized_blocks->end();
127 std::map<code_block *, cell> new_uninitialized_blocks;
128 for(; iter != end; iter++)
130 new_uninitialized_blocks.insert(std::make_pair(
131 visitor(iter->first),
135 parent->code->uninitialized_blocks = new_uninitialized_blocks;