]> gitweb.factorcode.org Git - factor.git/blob - vm/code_heap.cpp
vm: verifier shouldn't check for leftovers
[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 > ((u64)1 << (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 code_heap constructor",size);
11
12         cell start = seg->start + getpagesize() + seh_area_size;
13
14         allocator = new free_list_allocator<code_block>(seg->end - start,start);
15
16         /* See os-windows-x86.64.cpp for seh_area usage */
17         safepoint_page = (void *)seg->start;
18         seh_area = (char *)seg->start + getpagesize();
19 }
20
21 code_heap::~code_heap()
22 {
23         delete allocator;
24         allocator = NULL;
25         delete seg;
26         seg = NULL;
27 }
28
29 void code_heap::write_barrier(code_block *compiled)
30 {
31         points_to_nursery.insert(compiled);
32         points_to_aging.insert(compiled);
33 }
34
35 void code_heap::clear_remembered_set()
36 {
37         points_to_nursery.clear();
38         points_to_aging.clear();
39 }
40
41 bool code_heap::uninitialized_p(code_block *compiled)
42 {
43         return uninitialized_blocks.count(compiled) > 0;
44 }
45
46 bool code_heap::marked_p(code_block *compiled)
47 {
48         return allocator->state.marked_p(compiled);
49 }
50
51 void code_heap::set_marked_p(code_block *compiled)
52 {
53         allocator->state.set_marked_p(compiled);
54 }
55
56 void code_heap::clear_mark_bits()
57 {
58         allocator->state.clear_mark_bits();
59 }
60
61 void code_heap::free(code_block *compiled)
62 {
63         FACTOR_ASSERT(!uninitialized_p(compiled));
64         points_to_nursery.erase(compiled);
65         points_to_aging.erase(compiled);
66         all_blocks.erase(compiled);
67         allocator->free(compiled);
68 }
69
70 void code_heap::flush_icache()
71 {
72         factor::flush_icache(seg->start,seg->size);
73 }
74
75 struct clear_free_blocks_from_all_blocks_iterator
76 {
77         code_heap *code;
78
79         clear_free_blocks_from_all_blocks_iterator(code_heap *code) : code(code) {}
80
81         void operator()(code_block *free_block, cell size) {
82                 std::set<code_block*>::iterator erase_from =
83                         code->all_blocks.lower_bound(free_block);
84                 std::set<code_block*>::iterator erase_to =
85                         code->all_blocks.lower_bound((code_block*)((char*)free_block + size));
86
87                 code->all_blocks.erase(erase_from, erase_to);
88         }
89 };
90
91 void code_heap::sweep()
92 {
93         clear_free_blocks_from_all_blocks_iterator clearer(this);
94         allocator->sweep(clearer);
95 #ifdef FACTOR_DEBUG
96         verify_all_blocks_set();
97 #endif
98 }
99
100 struct all_blocks_set_verifier {
101         std::set<code_block*> *all_blocks;
102
103         all_blocks_set_verifier(std::set<code_block*> *all_blocks) : all_blocks(all_blocks) {}
104
105         void operator()(code_block *block, cell size)
106         {
107                 FACTOR_ASSERT(all_blocks->find(block) != all_blocks->end());
108                 all_blocks->erase(block);
109         }
110 };
111
112 void code_heap::verify_all_blocks_set()
113 {
114         all_blocks_set_verifier verifier(&all_blocks);
115         allocator->iterate(verifier);
116 }
117
118 code_block *code_heap::code_block_for_address(cell address)
119 {
120         std::set<code_block*>::const_iterator blocki =
121                 all_blocks.upper_bound((code_block*)address);
122         FACTOR_ASSERT(blocki != all_blocks.begin());
123         --blocki;
124         code_block* found_block = *blocki;
125 #ifdef FACTOR_DEBUG
126         if (!((cell)found_block->entry_point() <= address
127                 && address - (cell)found_block->entry_point() < found_block->size()))
128         {
129                 std::cerr << "invalid block found in all_blocks set!" << std::endl;
130                 std::cerr << "address " << (void*)address
131                         << " block " << (void*)found_block
132                         << " entry point " << (void*)found_block->entry_point()
133                         << " size " << found_block->size()
134                         << " free? " << found_block->free_p();
135                 verify_all_blocks_set();
136                 FACTOR_ASSERT(false);
137         }
138 #endif
139         return found_block;
140 }
141
142 struct all_blocks_set_inserter {
143         code_heap *code;
144
145         all_blocks_set_inserter(code_heap *code) : code(code) {}
146
147         void operator()(code_block *block, cell size)
148         {
149                 code->all_blocks.insert(block);
150         }
151 };
152
153 void code_heap::initialize_all_blocks_set()
154 {
155         all_blocks.clear();
156         all_blocks_set_inserter inserter(this);
157         allocator->iterate(inserter);
158 }
159
160 /* Allocate a code heap during startup */
161 void factor_vm::init_code_heap(cell size)
162 {
163         code = new code_heap(size);
164 }
165
166 struct word_updater {
167         factor_vm *parent;
168         bool reset_inline_caches;
169
170         word_updater(factor_vm *parent_, bool reset_inline_caches_) :
171                 parent(parent_), reset_inline_caches(reset_inline_caches_) {}
172
173         void operator()(code_block *compiled, cell size)
174         {
175                 parent->update_word_references(compiled,reset_inline_caches);
176         }
177 };
178
179 /* Update pointers to words referenced from all code blocks.
180 Only needed after redefining an existing word.
181 If generic words were redefined, inline caches need to be reset. */
182 void factor_vm::update_code_heap_words(bool reset_inline_caches)
183 {
184         word_updater updater(this,reset_inline_caches);
185         each_code_block(updater);
186 }
187
188 /* Fix up new words only.
189 Fast path for compilation units that only define new words. */
190 void factor_vm::initialize_code_blocks()
191 {
192         std::map<code_block *, cell>::const_iterator iter = code->uninitialized_blocks.begin();
193         std::map<code_block *, cell>::const_iterator end = code->uninitialized_blocks.end();
194
195         for(; iter != end; iter++)
196                 initialize_code_block(iter->first,iter->second);
197
198         code->uninitialized_blocks.clear();
199 }
200
201 void factor_vm::primitive_modify_code_heap()
202 {
203         bool reset_inline_caches = to_boolean(ctx->pop());
204         bool update_existing_words = to_boolean(ctx->pop());
205         data_root<array> alist(ctx->pop(),this);
206
207         cell count = array_capacity(alist.untagged());
208
209         if(count == 0)
210                 return;
211
212         for(cell i = 0; i < count; i++)
213         {
214                 data_root<array> pair(array_nth(alist.untagged(),i),this);
215
216                 data_root<word> word(array_nth(pair.untagged(),0),this);
217                 data_root<object> data(array_nth(pair.untagged(),1),this);
218
219                 switch(data.type())
220                 {
221                 case QUOTATION_TYPE:
222                         jit_compile_word(word.value(),data.value(),false);
223                         break;
224                 case ARRAY_TYPE:
225                         {
226                                 array *compiled_data = data.as<array>().untagged();
227                                 cell parameters = array_nth(compiled_data,0);
228                                 cell literals = array_nth(compiled_data,1);
229                                 cell relocation = array_nth(compiled_data,2);
230                                 cell labels = array_nth(compiled_data,3);
231                                 cell code = array_nth(compiled_data,4);
232                                 cell frame_size = untag_fixnum(array_nth(compiled_data,5));
233
234                                 code_block *compiled = add_code_block(
235                                         code_block_optimized,
236                                         code,
237                                         labels,
238                                         word.value(),
239                                         relocation,
240                                         parameters,
241                                         literals,
242                                         frame_size);
243
244                                 word->entry_point = compiled->entry_point();
245                         }
246                         break;
247                 default:
248                         critical_error("Expected a quotation or an array",data.value());
249                         break;
250                 }
251         }
252
253         if(update_existing_words)
254                 update_code_heap_words(reset_inline_caches);
255         else
256                 initialize_code_blocks();
257 }
258
259 code_heap_room factor_vm::code_room()
260 {
261         code_heap_room room;
262
263         room.size             = code->allocator->size;
264         room.occupied_space   = code->allocator->occupied_space();
265         room.total_free       = code->allocator->free_space();
266         room.contiguous_free  = code->allocator->largest_free_block();
267         room.free_block_count = code->allocator->free_block_count();
268
269         return room;
270 }
271
272 void factor_vm::primitive_code_room()
273 {
274         code_heap_room room = code_room();
275         ctx->push(tag<byte_array>(byte_array_from_value(&room)));
276 }
277
278 struct stack_trace_stripper {
279         explicit stack_trace_stripper() {}
280
281         void operator()(code_block *compiled, cell size)
282         {
283                 compiled->owner = false_object;
284         }
285 };
286
287 void factor_vm::primitive_strip_stack_traces()
288 {
289         stack_trace_stripper stripper;
290         each_code_block(stripper);
291 }
292
293 struct code_block_accumulator {
294         std::vector<cell> objects;
295
296         void operator()(code_block *compiled, cell size)
297         {
298                 objects.push_back(compiled->owner);
299                 objects.push_back(compiled->parameters);
300                 objects.push_back(compiled->relocation);
301
302                 objects.push_back(tag_fixnum(compiled->type()));
303                 objects.push_back(tag_fixnum(compiled->size()));
304
305                 /* Note: the entry point is always a multiple of the heap
306                 alignment (16 bytes). We cannot allocate while iterating
307                 through the code heap, so it is not possible to call
308                 from_unsigned_cell() here. It is OK, however, to add it as
309                 if it were a fixnum, and have library code shift it to the
310                 left by 4. */
311                 cell entry_point = (cell)compiled->entry_point();
312                 FACTOR_ASSERT((entry_point & (data_alignment - 1)) == 0);
313                 FACTOR_ASSERT((entry_point & TAG_MASK) == FIXNUM_TYPE);
314                 objects.push_back(entry_point);
315         }
316 };
317
318 cell factor_vm::code_blocks()
319 {
320         code_block_accumulator accum;
321         each_code_block(accum);
322         return std_vector_to_array(accum.objects);
323 }
324
325 void factor_vm::primitive_code_blocks()
326 {
327         ctx->push(code_blocks());
328 }
329
330 }