]> gitweb.factorcode.org Git - factor.git/blob - vm/code_heap.cpp
vm: replace assert with FACTOR_ASSERT
[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 code_block *code_heap::code_block_for_address(cell address)
76 {
77         std::set<code_block*>::const_iterator blocki =
78                 all_blocks.upper_bound((code_block*)address);
79         FACTOR_ASSERT(blocki != all_blocks.begin());
80         --blocki;
81         code_block* found_block = *blocki;
82         FACTOR_ASSERT((cell)found_block->entry_point() <= address
83                 && address - (cell)found_block->entry_point() < found_block->size());
84         return found_block;
85 }
86
87 struct all_blocks_set_inserter {
88         code_heap *code;
89
90         all_blocks_set_inserter(code_heap *code) : code(code) {}
91
92         void operator()(code_block *block, cell size)
93         {
94                 code->all_blocks.insert(block);
95         }
96 };
97
98 void code_heap::initialize_all_blocks_set()
99 {
100         all_blocks.clear();
101         all_blocks_set_inserter inserter(this);
102         allocator->iterate(inserter);
103 }
104
105 void code_heap::update_all_blocks_set(mark_bits<code_block> *code_forwarding_map)
106 {
107         std::set<code_block *> new_all_blocks;
108         for (std::set<code_block *>::const_iterator oldi = all_blocks.begin();
109                 oldi != all_blocks.end();
110                 ++oldi)
111         {
112                 code_block *new_block = code_forwarding_map->forward_block(*oldi);
113                 new_all_blocks.insert(new_block);
114         }
115         all_blocks.swap(new_all_blocks);
116 }
117
118 /* Allocate a code heap during startup */
119 void factor_vm::init_code_heap(cell size)
120 {
121         code = new code_heap(size);
122 }
123
124 struct word_updater {
125         factor_vm *parent;
126         bool reset_inline_caches;
127
128         word_updater(factor_vm *parent_, bool reset_inline_caches_) :
129                 parent(parent_), reset_inline_caches(reset_inline_caches_) {}
130
131         void operator()(code_block *compiled, cell size)
132         {
133                 parent->update_word_references(compiled,reset_inline_caches);
134         }
135 };
136
137 /* Update pointers to words referenced from all code blocks.
138 Only needed after redefining an existing word.
139 If generic words were redefined, inline caches need to be reset. */
140 void factor_vm::update_code_heap_words(bool reset_inline_caches)
141 {
142         word_updater updater(this,reset_inline_caches);
143         each_code_block(updater);
144 }
145
146 /* Fix up new words only.
147 Fast path for compilation units that only define new words. */
148 void factor_vm::initialize_code_blocks()
149 {
150         std::map<code_block *, cell>::const_iterator iter = code->uninitialized_blocks.begin();
151         std::map<code_block *, cell>::const_iterator end = code->uninitialized_blocks.end();
152
153         for(; iter != end; iter++)
154                 initialize_code_block(iter->first,iter->second);
155
156         code->uninitialized_blocks.clear();
157 }
158
159 void factor_vm::primitive_modify_code_heap()
160 {
161         bool reset_inline_caches = to_boolean(ctx->pop());
162         bool update_existing_words = to_boolean(ctx->pop());
163         data_root<array> alist(ctx->pop(),this);
164
165         cell count = array_capacity(alist.untagged());
166
167         if(count == 0)
168                 return;
169
170         for(cell i = 0; i < count; i++)
171         {
172                 data_root<array> pair(array_nth(alist.untagged(),i),this);
173
174                 data_root<word> word(array_nth(pair.untagged(),0),this);
175                 data_root<object> data(array_nth(pair.untagged(),1),this);
176
177                 switch(data.type())
178                 {
179                 case QUOTATION_TYPE:
180                         jit_compile_word(word.value(),data.value(),false);
181                         break;
182                 case ARRAY_TYPE:
183                         {
184                                 array *compiled_data = data.as<array>().untagged();
185                                 cell parameters = array_nth(compiled_data,0);
186                                 cell literals = array_nth(compiled_data,1);
187                                 cell relocation = array_nth(compiled_data,2);
188                                 cell labels = array_nth(compiled_data,3);
189                                 cell code = array_nth(compiled_data,4);
190
191                                 code_block *compiled = add_code_block(
192                                         code_block_optimized,
193                                         code,
194                                         labels,
195                                         word.value(),
196                                         relocation,
197                                         parameters,
198                                         literals);
199
200                                 word->entry_point = compiled->entry_point();
201                         }
202                         break;
203                 default:
204                         critical_error("Expected a quotation or an array",data.value());
205                         break;
206                 }
207         }
208
209         if(update_existing_words)
210                 update_code_heap_words(reset_inline_caches);
211         else
212                 initialize_code_blocks();
213 }
214
215 code_heap_room factor_vm::code_room()
216 {
217         code_heap_room room;
218
219         room.size             = code->allocator->size;
220         room.occupied_space   = code->allocator->occupied_space();
221         room.total_free       = code->allocator->free_space();
222         room.contiguous_free  = code->allocator->largest_free_block();
223         room.free_block_count = code->allocator->free_block_count();
224
225         return room;
226 }
227
228 void factor_vm::primitive_code_room()
229 {
230         code_heap_room room = code_room();
231         ctx->push(tag<byte_array>(byte_array_from_value(&room)));
232 }
233
234 struct stack_trace_stripper {
235         explicit stack_trace_stripper() {}
236
237         void operator()(code_block *compiled, cell size)
238         {
239                 compiled->owner = false_object;
240         }
241 };
242
243 void factor_vm::primitive_strip_stack_traces()
244 {
245         stack_trace_stripper stripper;
246         each_code_block(stripper);
247 }
248
249 struct code_block_accumulator {
250         std::vector<cell> objects;
251
252         void operator()(code_block *compiled, cell size)
253         {
254                 objects.push_back(compiled->owner);
255                 objects.push_back(compiled->parameters);
256                 objects.push_back(compiled->relocation);
257
258                 objects.push_back(tag_fixnum(compiled->type()));
259                 objects.push_back(tag_fixnum(compiled->size()));
260
261                 /* Note: the entry point is always a multiple of the heap
262                 alignment (16 bytes). We cannot allocate while iterating
263                 through the code heap, so it is not possible to call
264                 from_unsigned_cell() here. It is OK, however, to add it as
265                 if it were a fixnum, and have library code shift it to the
266                 left by 4. */
267                 cell entry_point = (cell)compiled->entry_point();
268                 FACTOR_ASSERT((entry_point & (data_alignment - 1)) == 0);
269                 FACTOR_ASSERT((entry_point & TAG_MASK) == FIXNUM_TYPE);
270                 objects.push_back(entry_point);
271         }
272 };
273
274 cell factor_vm::code_blocks()
275 {
276         code_block_accumulator accum;
277         each_code_block(accum);
278         return std_vector_to_array(accum.objects);
279 }
280
281 void factor_vm::primitive_code_blocks()
282 {
283         ctx->push(code_blocks());
284 }
285
286 }