6 code_heap::code_heap(cell size)
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);
12 cell start = seg->start + getpagesize() + seh_area_size;
14 allocator = new free_list_allocator<code_block>(seg->end - start,start);
16 /* See os-windows-x86.64.cpp for seh_area usage */
17 safepoint_page = (void *)seg->start;
18 seh_area = (char *)seg->start + getpagesize();
21 code_heap::~code_heap()
29 void code_heap::write_barrier(code_block *compiled)
31 points_to_nursery.insert(compiled);
32 points_to_aging.insert(compiled);
35 void code_heap::clear_remembered_set()
37 points_to_nursery.clear();
38 points_to_aging.clear();
41 bool code_heap::uninitialized_p(code_block *compiled)
43 return uninitialized_blocks.count(compiled) > 0;
46 bool code_heap::marked_p(code_block *compiled)
48 return allocator->state.marked_p(compiled);
51 void code_heap::set_marked_p(code_block *compiled)
53 allocator->state.set_marked_p(compiled);
56 void code_heap::clear_mark_bits()
58 allocator->state.clear_mark_bits();
61 void code_heap::free(code_block *compiled)
63 FACTOR_ASSERT(!uninitialized_p(compiled));
64 points_to_nursery.erase(compiled);
65 points_to_aging.erase(compiled);
66 all_blocks.erase(compiled);
67 allocator->free(compiled);
70 void code_heap::flush_icache()
72 factor::flush_icache(seg->start,seg->size);
75 struct all_blocks_set_verifier {
76 std::set<code_block*> *leftovers;
78 all_blocks_set_verifier(std::set<code_block*> *leftovers) : leftovers(leftovers) {}
80 void operator()(code_block *block, cell size)
82 FACTOR_ASSERT(leftovers->find(block) != leftovers->end());
83 leftovers->erase(block);
87 void code_heap::verify_all_blocks_set()
89 std::set<code_block*> leftovers = all_blocks;
90 all_blocks_set_verifier verifier(&leftovers);
91 allocator->iterate(verifier);
92 FACTOR_ASSERT(leftovers.empty());
95 code_block *code_heap::code_block_for_address(cell address)
97 std::set<code_block*>::const_iterator blocki =
98 all_blocks.upper_bound((code_block*)address);
99 FACTOR_ASSERT(blocki != all_blocks.begin());
101 code_block* found_block = *blocki;
103 if (!((cell)found_block->entry_point() <= address
104 && address - (cell)found_block->entry_point() < found_block->size()))
106 std::cerr << "invalid block found in all_blocks set!" << std::endl;
107 verify_all_blocks_set();
108 FACTOR_ASSERT(false);
114 struct all_blocks_set_inserter {
117 all_blocks_set_inserter(code_heap *code) : code(code) {}
119 void operator()(code_block *block, cell size)
121 code->all_blocks.insert(block);
125 void code_heap::initialize_all_blocks_set()
128 all_blocks_set_inserter inserter(this);
129 allocator->iterate(inserter);
132 void code_heap::update_all_blocks_set(mark_bits<code_block> *code_forwarding_map)
134 std::set<code_block *> new_all_blocks;
135 for (std::set<code_block *>::const_iterator oldi = all_blocks.begin();
136 oldi != all_blocks.end();
139 code_block *new_block = code_forwarding_map->forward_block(*oldi);
140 new_all_blocks.insert(new_block);
142 all_blocks.swap(new_all_blocks);
145 /* Allocate a code heap during startup */
146 void factor_vm::init_code_heap(cell size)
148 code = new code_heap(size);
151 struct word_updater {
153 bool reset_inline_caches;
155 word_updater(factor_vm *parent_, bool reset_inline_caches_) :
156 parent(parent_), reset_inline_caches(reset_inline_caches_) {}
158 void operator()(code_block *compiled, cell size)
160 parent->update_word_references(compiled,reset_inline_caches);
164 /* Update pointers to words referenced from all code blocks.
165 Only needed after redefining an existing word.
166 If generic words were redefined, inline caches need to be reset. */
167 void factor_vm::update_code_heap_words(bool reset_inline_caches)
169 word_updater updater(this,reset_inline_caches);
170 each_code_block(updater);
173 /* Fix up new words only.
174 Fast path for compilation units that only define new words. */
175 void factor_vm::initialize_code_blocks()
177 std::map<code_block *, cell>::const_iterator iter = code->uninitialized_blocks.begin();
178 std::map<code_block *, cell>::const_iterator end = code->uninitialized_blocks.end();
180 for(; iter != end; iter++)
181 initialize_code_block(iter->first,iter->second);
183 code->uninitialized_blocks.clear();
186 void factor_vm::primitive_modify_code_heap()
188 bool reset_inline_caches = to_boolean(ctx->pop());
189 bool update_existing_words = to_boolean(ctx->pop());
190 data_root<array> alist(ctx->pop(),this);
192 cell count = array_capacity(alist.untagged());
197 for(cell i = 0; i < count; i++)
199 data_root<array> pair(array_nth(alist.untagged(),i),this);
201 data_root<word> word(array_nth(pair.untagged(),0),this);
202 data_root<object> data(array_nth(pair.untagged(),1),this);
207 jit_compile_word(word.value(),data.value(),false);
211 array *compiled_data = data.as<array>().untagged();
212 cell parameters = array_nth(compiled_data,0);
213 cell literals = array_nth(compiled_data,1);
214 cell relocation = array_nth(compiled_data,2);
215 cell labels = array_nth(compiled_data,3);
216 cell code = array_nth(compiled_data,4);
218 code_block *compiled = add_code_block(
219 code_block_optimized,
227 word->entry_point = compiled->entry_point();
231 critical_error("Expected a quotation or an array",data.value());
236 if(update_existing_words)
237 update_code_heap_words(reset_inline_caches);
239 initialize_code_blocks();
242 code_heap_room factor_vm::code_room()
246 room.size = code->allocator->size;
247 room.occupied_space = code->allocator->occupied_space();
248 room.total_free = code->allocator->free_space();
249 room.contiguous_free = code->allocator->largest_free_block();
250 room.free_block_count = code->allocator->free_block_count();
255 void factor_vm::primitive_code_room()
257 code_heap_room room = code_room();
258 ctx->push(tag<byte_array>(byte_array_from_value(&room)));
261 struct stack_trace_stripper {
262 explicit stack_trace_stripper() {}
264 void operator()(code_block *compiled, cell size)
266 compiled->owner = false_object;
270 void factor_vm::primitive_strip_stack_traces()
272 stack_trace_stripper stripper;
273 each_code_block(stripper);
276 struct code_block_accumulator {
277 std::vector<cell> objects;
279 void operator()(code_block *compiled, cell size)
281 objects.push_back(compiled->owner);
282 objects.push_back(compiled->parameters);
283 objects.push_back(compiled->relocation);
285 objects.push_back(tag_fixnum(compiled->type()));
286 objects.push_back(tag_fixnum(compiled->size()));
288 /* Note: the entry point is always a multiple of the heap
289 alignment (16 bytes). We cannot allocate while iterating
290 through the code heap, so it is not possible to call
291 from_unsigned_cell() here. It is OK, however, to add it as
292 if it were a fixnum, and have library code shift it to the
294 cell entry_point = (cell)compiled->entry_point();
295 FACTOR_ASSERT((entry_point & (data_alignment - 1)) == 0);
296 FACTOR_ASSERT((entry_point & TAG_MASK) == FIXNUM_TYPE);
297 objects.push_back(entry_point);
301 cell factor_vm::code_blocks()
303 code_block_accumulator accum;
304 each_code_block(accum);
305 return std_vector_to_array(accum.objects);
308 void factor_vm::primitive_code_blocks()
310 ctx->push(code_blocks());