]> gitweb.factorcode.org Git - factor.git/blob - vm/code_heap.cpp
Invalidate inline caches used by call( and execute( when words are redefined
[factor.git] / vm / code_heap.cpp
1 #include "master.hpp"
2
3 namespace factor
4 {
5
6 code_heap::code_heap(cell size)
7 {
8         if(size > (1L << (sizeof(cell) * 8 - 6))) fatal_error("Heap too large",size);
9         seg = new segment(align_page(size),true);
10         if(!seg) fatal_error("Out of memory in heap allocator",size);
11         allocator = new free_list_allocator<code_block>(size,seg->start);
12 }
13
14 code_heap::~code_heap()
15 {
16         delete allocator;
17         allocator = NULL;
18         delete seg;
19         seg = NULL;
20 }
21
22 void code_heap::write_barrier(code_block *compiled)
23 {
24         points_to_nursery.insert(compiled);
25         points_to_aging.insert(compiled);
26 }
27
28 void code_heap::clear_remembered_set()
29 {
30         points_to_nursery.clear();
31         points_to_aging.clear();
32 }
33
34 bool code_heap::needs_fixup_p(code_block *compiled)
35 {
36         return needs_fixup.count(compiled) > 0;
37 }
38
39 bool code_heap::marked_p(code_block *compiled)
40 {
41         return allocator->state.marked_p(compiled);
42 }
43
44 void code_heap::set_marked_p(code_block *compiled)
45 {
46         allocator->state.set_marked_p(compiled);
47 }
48
49 void code_heap::clear_mark_bits()
50 {
51         allocator->state.clear_mark_bits();
52 }
53
54 void code_heap::code_heap_free(code_block *compiled)
55 {
56         points_to_nursery.erase(compiled);
57         points_to_aging.erase(compiled);
58         needs_fixup.erase(compiled);
59         allocator->free(compiled);
60 }
61
62 /* Allocate a code heap during startup */
63 void factor_vm::init_code_heap(cell size)
64 {
65         code = new code_heap(size);
66 }
67
68 bool factor_vm::in_code_heap_p(cell ptr)
69 {
70         return (ptr >= code->seg->start && ptr <= code->seg->end);
71 }
72
73 /* Compile a word definition with the non-optimizing compiler. Allocates memory */
74 void factor_vm::jit_compile_word(cell word_, cell def_, bool relocate)
75 {
76         data_root<word> word(word_,this);
77         data_root<quotation> def(def_,this);
78
79         jit_compile(def.value(),relocate);
80
81         word->code = def->code;
82
83         if(to_boolean(word->pic_def)) jit_compile(word->pic_def,relocate);
84         if(to_boolean(word->pic_tail_def)) jit_compile(word->pic_tail_def,relocate);
85 }
86
87 struct word_updater {
88         factor_vm *parent;
89
90         explicit word_updater(factor_vm *parent_) : parent(parent_) {}
91
92         void operator()(code_block *compiled, cell size)
93         {
94                 parent->update_word_references(compiled);
95         }
96 };
97
98 /* Update pointers to words referenced from all code blocks. Only after
99 defining a new word. */
100 void factor_vm::update_code_heap_words()
101 {
102         word_updater updater(this);
103         iterate_code_heap(updater);
104 }
105
106 /* After a full GC that did not grow the heap, we have to update references
107 to literals and other words. */
108 struct word_and_literal_code_heap_updater {
109         factor_vm *parent;
110
111         explicit word_and_literal_code_heap_updater(factor_vm *parent_) : parent(parent_) {}
112
113         void operator()(code_block *block, cell size)
114         {
115                 parent->update_code_block_words_and_literals(block);
116         }
117 };
118
119 void factor_vm::update_code_heap_words_and_literals()
120 {
121         word_and_literal_code_heap_updater updater(this);
122         iterate_code_heap(updater);
123 }
124
125 /* After growing the heap, we have to perform a full relocation to update
126 references to card and deck arrays. */
127 struct code_heap_relocator {
128         factor_vm *parent;
129
130         explicit code_heap_relocator(factor_vm *parent_) : parent(parent_) {}
131
132         void operator()(code_block *block, cell size)
133         {
134                 parent->relocate_code_block(block);
135         }
136 };
137
138 void factor_vm::increment_definition_counter()
139 {
140         /* Increment redefinition counter for call( */
141         cell counter_ = special_objects[REDEFINITION_COUNTER];
142         cell counter;
143         if(counter_ == false_object)
144                 counter = 0;
145         else
146                 counter = untag_fixnum(counter_) + 1;
147         special_objects[REDEFINITION_COUNTER] = tag_fixnum(counter);
148 }
149
150 void factor_vm::primitive_modify_code_heap()
151 {
152         data_root<array> alist(dpop(),this);
153
154         cell count = array_capacity(alist.untagged());
155
156         if(count == 0)
157                 return;
158
159         for(cell i = 0; i < count; i++)
160         {
161                 data_root<array> pair(array_nth(alist.untagged(),i),this);
162
163                 data_root<word> word(array_nth(pair.untagged(),0),this);
164                 data_root<object> data(array_nth(pair.untagged(),1),this);
165
166                 switch(data.type())
167                 {
168                 case QUOTATION_TYPE:
169                         jit_compile_word(word.value(),data.value(),false);
170                         break;
171                 case ARRAY_TYPE:
172                         {
173                                 array *compiled_data = data.as<array>().untagged();
174                                 cell owner = array_nth(compiled_data,0);
175                                 cell literals = array_nth(compiled_data,1);
176                                 cell relocation = array_nth(compiled_data,2);
177                                 cell labels = array_nth(compiled_data,3);
178                                 cell code = array_nth(compiled_data,4);
179
180                                 code_block *compiled = add_code_block(
181                                         code_block_optimized,
182                                         code,
183                                         labels,
184                                         owner,
185                                         relocation,
186                                         literals);
187
188                                 word->code = compiled;
189                         }
190                         break;
191                 default:
192                         critical_error("Expected a quotation or an array",data.value());
193                         break;
194                 }
195
196                 update_word_xt(word.untagged());
197         }
198
199         update_code_heap_words();
200         increment_definition_counter();
201 }
202
203 code_heap_room factor_vm::code_room()
204 {
205         code_heap_room room;
206
207         room.size             = code->allocator->size;
208         room.occupied_space   = code->allocator->occupied_space();
209         room.total_free       = code->allocator->free_space();
210         room.contiguous_free  = code->allocator->largest_free_block();
211         room.free_block_count = code->allocator->free_block_count();
212
213         return room;
214 }
215
216 void factor_vm::primitive_code_room()
217 {
218         code_heap_room room = code_room();
219         dpush(tag<byte_array>(byte_array_from_value(&room)));
220 }
221
222 struct stack_trace_stripper {
223         explicit stack_trace_stripper() {}
224
225         void operator()(code_block *compiled, cell size)
226         {
227                 compiled->owner = false_object;
228         }
229 };
230
231 void factor_vm::primitive_strip_stack_traces()
232 {
233         stack_trace_stripper stripper;
234         iterate_code_heap(stripper);
235 }
236
237 }