]> gitweb.factorcode.org Git - factor.git/blob - vm/code_block_visitor.hpp
d43daa3ad3d5b65e322b821acdd0ff9472167c50
[factor.git] / vm / code_block_visitor.hpp
1 namespace factor
2 {
3
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.
7
8 This is used by GC's sweep and compact phases, and the implementation of the
9 modify-code-heap primitive.
10
11 Iteration is driven by visit_*() methods. Some of them define GC roots:
12 - visit_context_code_blocks()
13 - visit_callback_code_blocks() */
14  
15 template<typename Fixup> struct code_block_visitor {
16         factor_vm *parent;
17         Fixup fixup;
18
19         explicit code_block_visitor(factor_vm *parent_, Fixup fixup_) :
20                 parent(parent_), fixup(fixup_) {}
21
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();
27
28         void visit_code_roots();
29 };
30
31 template<typename Fixup>
32 code_block *code_block_visitor<Fixup>::visit_code_block(code_block *compiled)
33 {
34         return fixup.fixup_code(compiled);
35 }
36
37 template<typename Fixup>
38 struct call_frame_code_block_visitor {
39         factor_vm *parent;
40         Fixup fixup;
41
42         explicit call_frame_code_block_visitor(factor_vm *parent_, Fixup fixup_) :
43                 parent(parent_), fixup(fixup_) {}
44
45         void operator()(stack_frame *frame)
46         {
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);
51         }
52 };
53
54 template<typename Fixup>
55 void code_block_visitor<Fixup>::visit_object_code_block(object *obj)
56 {
57         switch(obj->type())
58         {
59         case WORD_TYPE:
60                 {
61                         word *w = (word *)obj;
62                         if(w->code)
63                                 w->code = visit_code_block(w->code);
64                         if(w->counting_profiler)
65                                 w->counting_profiler = visit_code_block(w->counting_profiler);
66
67                         parent->update_word_entry_point(w);
68                         break;
69                 }
70         case QUOTATION_TYPE:
71                 {
72                         quotation *q = (quotation *)obj;
73                         if(q->code)
74                                 parent->set_quot_entry_point(q,visit_code_block(q->code));
75                         break;
76                 }
77         case CALLSTACK_TYPE:
78                 {
79                         callstack *stack = (callstack *)obj;
80                         call_frame_code_block_visitor<Fixup> call_frame_visitor(parent,fixup);
81                         parent->iterate_callstack_object(stack,call_frame_visitor);
82                         break;
83                 }
84         }
85 }
86
87 template<typename Fixup>
88 struct embedded_code_pointers_visitor {
89         Fixup fixup;
90
91         explicit embedded_code_pointers_visitor(Fixup fixup_) : fixup(fixup_) {}
92
93         void operator()(instruction_operand op)
94         {
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(fixup.fixup_code(op.load_code_block()));
100         }
101 };
102
103 template<typename Fixup>
104 void code_block_visitor<Fixup>::visit_embedded_code_pointers(code_block *compiled)
105 {
106         if(!parent->code->uninitialized_p(compiled))
107         {
108                 embedded_code_pointers_visitor<Fixup> operand_visitor(fixup);
109                 compiled->each_instruction_operand(operand_visitor);
110         }
111 }
112
113 template<typename Fixup>
114 void code_block_visitor<Fixup>::visit_context_code_blocks()
115 {
116         call_frame_code_block_visitor<Fixup> call_frame_visitor(parent,fixup);
117         parent->iterate_active_callstacks(call_frame_visitor);
118 }
119
120 template<typename Fixup>
121 void code_block_visitor<Fixup>::visit_uninitialized_code_blocks()
122 {
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();
126
127         std::map<code_block *, cell> new_uninitialized_blocks;
128         for(; iter != end; iter++)
129         {
130                 new_uninitialized_blocks.insert(std::make_pair(
131                         fixup.fixup_code(iter->first),
132                         iter->second));
133         }
134
135         parent->code->uninitialized_blocks = new_uninitialized_blocks;
136 }
137
138 template<typename Fixup>
139 void code_block_visitor<Fixup>::visit_code_roots()
140 {
141         visit_uninitialized_code_blocks();
142 }
143
144 }