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