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