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