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