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