]> gitweb.factorcode.org Git - factor.git/blob - vm/code_heap.cpp
VM: Remove unnecessary explicit keywords
[factor.git] / vm / code_heap.cpp
1 #include "master.hpp"
2
3 namespace factor {
4
5 code_heap::code_heap(cell size) {
6   if (size > ((u64) 1 << (sizeof(cell) * 8 - 6)))
7     fatal_error("Heap too large", size);
8   seg = new segment(align_page(size), true);
9   if (!seg)
10     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   delete allocator;
23   allocator = NULL;
24   delete seg;
25   seg = NULL;
26 }
27
28 void code_heap::write_barrier(code_block* compiled) {
29   points_to_nursery.insert(compiled);
30   points_to_aging.insert(compiled);
31 }
32
33 void code_heap::clear_remembered_set() {
34   points_to_nursery.clear();
35   points_to_aging.clear();
36 }
37
38 bool code_heap::uninitialized_p(code_block* compiled) {
39   return uninitialized_blocks.count(compiled) > 0;
40 }
41
42 bool code_heap::marked_p(code_block* compiled) {
43   return allocator->state.marked_p(compiled);
44 }
45
46 void code_heap::set_marked_p(code_block* compiled) {
47   allocator->state.set_marked_p(compiled);
48 }
49
50 void code_heap::clear_mark_bits() { allocator->state.clear_mark_bits(); }
51
52 void code_heap::free(code_block* compiled) {
53   FACTOR_ASSERT(!uninitialized_p(compiled));
54   points_to_nursery.erase(compiled);
55   points_to_aging.erase(compiled);
56   all_blocks.erase((cell) compiled);
57   allocator->free(compiled);
58 }
59
60 void code_heap::flush_icache() { factor::flush_icache(seg->start, seg->size); }
61
62 struct clear_free_blocks_from_all_blocks_iterator {
63   code_heap* code;
64
65   clear_free_blocks_from_all_blocks_iterator(code_heap* code) : code(code) {}
66
67   void operator()(code_block* free_block, cell size) {
68     std::set<cell>::iterator erase_from =
69         code->all_blocks.lower_bound((cell) free_block);
70     std::set<cell>::iterator erase_to =
71         code->all_blocks.lower_bound((cell) free_block + size);
72
73     code->all_blocks.erase(erase_from, erase_to);
74   }
75 };
76
77 void code_heap::sweep() {
78   clear_free_blocks_from_all_blocks_iterator clearer(this);
79   allocator->sweep(clearer);
80 #ifdef FACTOR_DEBUG
81   verify_all_blocks_set();
82 #endif
83 }
84
85 struct all_blocks_set_verifier {
86   std::set<cell>* all_blocks;
87
88   all_blocks_set_verifier(std::set<cell>* all_blocks)
89       : all_blocks(all_blocks) {}
90
91   void operator()(code_block* block, cell size) {
92     FACTOR_ASSERT(all_blocks->find((cell) block) != all_blocks->end());
93   }
94 };
95
96 void code_heap::verify_all_blocks_set() {
97   all_blocks_set_verifier verifier(&all_blocks);
98   allocator->iterate(verifier);
99 }
100
101 code_block* code_heap::code_block_for_address(cell address) {
102   std::set<cell>::const_iterator blocki = all_blocks.upper_bound(address);
103   FACTOR_ASSERT(blocki != all_blocks.begin());
104   --blocki;
105   code_block* found_block = (code_block*)*blocki;
106   FACTOR_ASSERT((cell) found_block->entry_point() <=
107                 address /* XXX this isn't valid during fixup. should store the
108                                size in the map
109                               && address - (cell)found_block->entry_point() <
110                                  found_block->size()*/);
111   return found_block;
112 }
113
114 struct all_blocks_set_inserter {
115   code_heap* code;
116
117   all_blocks_set_inserter(code_heap* code) : code(code) {}
118
119   void operator()(code_block* block, cell size) {
120     code->all_blocks.insert((cell) block);
121   }
122 };
123
124 void code_heap::initialize_all_blocks_set() {
125   all_blocks.clear();
126   all_blocks_set_inserter inserter(this);
127   allocator->iterate(inserter);
128 #if defined(FACTOR_DEBUG)
129   verify_all_blocks_set();
130 #endif
131 }
132
133 /* Allocate a code heap during startup */
134 void factor_vm::init_code_heap(cell size) { code = new code_heap(size); }
135
136 struct word_updater {
137   factor_vm* parent;
138   bool reset_inline_caches;
139
140   word_updater(factor_vm* parent_, bool reset_inline_caches_)
141       : parent(parent_), reset_inline_caches(reset_inline_caches_) {}
142
143   void operator()(code_block* compiled, cell size) {
144     parent->update_word_references(compiled, reset_inline_caches);
145   }
146 };
147
148 /* Update pointers to words referenced from all code blocks.
149 Only needed after redefining an existing word.
150 If generic words were redefined, inline caches need to be reset. */
151 void factor_vm::update_code_heap_words(bool reset_inline_caches) {
152   word_updater updater(this, reset_inline_caches);
153   each_code_block(updater);
154 }
155
156 /* Fix up new words only.
157 Fast path for compilation units that only define new words. */
158 void factor_vm::initialize_code_blocks() {
159   std::map<code_block*, cell>::const_iterator iter =
160       code->uninitialized_blocks.begin();
161   std::map<code_block*, cell>::const_iterator end =
162       code->uninitialized_blocks.end();
163
164   for (; iter != end; iter++)
165     initialize_code_block(iter->first, iter->second);
166
167   code->uninitialized_blocks.clear();
168 }
169
170 /* Allocates memory */
171 void factor_vm::primitive_modify_code_heap() {
172   bool reset_inline_caches = to_boolean(ctx->pop());
173   bool update_existing_words = to_boolean(ctx->pop());
174   data_root<array> alist(ctx->pop(), this);
175
176   cell count = array_capacity(alist.untagged());
177
178   if (count == 0)
179     return;
180
181   for (cell i = 0; i < count; i++) {
182     data_root<array> pair(array_nth(alist.untagged(), i), this);
183
184     data_root<word> word(array_nth(pair.untagged(), 0), this);
185     data_root<object> data(array_nth(pair.untagged(), 1), this);
186
187     switch (data.type()) {
188       case QUOTATION_TYPE:
189         jit_compile_word(word.value(), data.value(), false);
190         break;
191       case ARRAY_TYPE: {
192         array* compiled_data = data.as<array>().untagged();
193         cell parameters = array_nth(compiled_data, 0);
194         cell literals = array_nth(compiled_data, 1);
195         cell relocation = array_nth(compiled_data, 2);
196         cell labels = array_nth(compiled_data, 3);
197         cell code = array_nth(compiled_data, 4);
198         cell frame_size = untag_fixnum(array_nth(compiled_data, 5));
199
200         code_block* compiled =
201             add_code_block(code_block_optimized, code, labels, word.value(),
202                            relocation, parameters, literals, frame_size);
203
204         word->entry_point = compiled->entry_point();
205       } break;
206       default:
207         critical_error("Expected a quotation or an array", data.value());
208         break;
209     }
210   }
211
212   if (update_existing_words)
213     update_code_heap_words(reset_inline_caches);
214   else
215     initialize_code_blocks();
216 }
217
218 code_heap_room factor_vm::code_room() {
219   code_heap_room room;
220
221   room.size = code->allocator->size;
222   room.occupied_space = code->allocator->occupied_space();
223   room.total_free = code->allocator->free_space();
224   room.contiguous_free = code->allocator->largest_free_block();
225   room.free_block_count = code->allocator->free_block_count();
226
227   return room;
228 }
229
230 /* Allocates memory */
231 void factor_vm::primitive_code_room() {
232   code_heap_room room = code_room();
233   ctx->push(tag<byte_array>(byte_array_from_value(&room)));
234 }
235
236 struct stack_trace_stripper {
237   stack_trace_stripper() {}
238
239   void operator()(code_block* compiled, cell size) {
240     compiled->owner = false_object;
241   }
242 };
243
244 void factor_vm::primitive_strip_stack_traces() {
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     objects.push_back(compiled->owner);
254     objects.push_back(compiled->parameters);
255     objects.push_back(compiled->relocation);
256
257     objects.push_back(tag_fixnum(compiled->type()));
258     objects.push_back(tag_fixnum(compiled->size()));
259
260     /* Note: the entry point is always a multiple of the heap
261        alignment (16 bytes). We cannot allocate while iterating
262        through the code heap, so it is not possible to call
263        from_unsigned_cell() here. It is OK, however, to add it as
264        if it were a fixnum, and have library code shift it to the
265        left by 4. */
266     cell entry_point = (cell) compiled->entry_point();
267     FACTOR_ASSERT((entry_point & (data_alignment - 1)) == 0);
268     FACTOR_ASSERT((entry_point & TAG_MASK) == FIXNUM_TYPE);
269     objects.push_back(entry_point);
270   }
271 };
272
273 /* Allocates memory */
274 cell factor_vm::code_blocks() {
275   code_block_accumulator accum;
276   each_code_block(accum);
277   return std_vector_to_array(accum.objects);
278 }
279
280 /* Allocates memory */
281 void factor_vm::primitive_code_blocks() { ctx->push(code_blocks()); }
282
283 }