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