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((cell)compiled);
67 allocator->free(compiled);
70 void code_heap::flush_icache()
72 factor::flush_icache(seg->start,seg->size);
75 struct clear_free_blocks_from_all_blocks_iterator
79 clear_free_blocks_from_all_blocks_iterator(code_heap *code) : code(code) {}
81 void operator()(code_block *free_block, cell size) {
82 std::set<cell>::iterator erase_from =
83 code->all_blocks.lower_bound((cell)free_block);
84 std::set<cell>::iterator erase_to =
85 code->all_blocks.lower_bound((cell)free_block + size);
87 code->all_blocks.erase(erase_from, erase_to);
91 void code_heap::sweep()
93 clear_free_blocks_from_all_blocks_iterator clearer(this);
94 allocator->sweep(clearer);
96 verify_all_blocks_set();
100 struct all_blocks_set_verifier {
101 std::set<cell> *all_blocks;
103 all_blocks_set_verifier(std::set<cell> *all_blocks) : all_blocks(all_blocks) {}
105 void operator()(code_block *block, cell size)
107 FACTOR_ASSERT(all_blocks->find((cell)block) != all_blocks->end());
111 void code_heap::verify_all_blocks_set()
113 all_blocks_set_verifier verifier(&all_blocks);
114 allocator->iterate(verifier);
117 code_block *code_heap::code_block_for_address(cell address)
119 std::set<cell>::const_iterator blocki =
120 all_blocks.upper_bound(address);
121 FACTOR_ASSERT(blocki != all_blocks.begin());
123 code_block* found_block = (code_block*)*blocki;
124 FACTOR_ASSERT((cell)found_block->entry_point() <= address
125 /* XXX this isn't valid during fixup. should store the size in the map
126 && address - (cell)found_block->entry_point() < found_block->size()*/);
130 struct all_blocks_set_inserter {
133 all_blocks_set_inserter(code_heap *code) : code(code) {}
135 void operator()(code_block *block, cell size)
137 code->all_blocks.insert((cell)block);
141 void code_heap::initialize_all_blocks_set()
144 all_blocks_set_inserter inserter(this);
145 allocator->iterate(inserter);
146 #if defined(FACTOR_DEBUG)
147 verify_all_blocks_set();
151 /* Allocate a code heap during startup */
152 void factor_vm::init_code_heap(cell size)
154 code = new code_heap(size);
157 struct word_updater {
159 bool reset_inline_caches;
161 word_updater(factor_vm *parent_, bool reset_inline_caches_) :
162 parent(parent_), reset_inline_caches(reset_inline_caches_) {}
164 void operator()(code_block *compiled, cell size)
166 parent->update_word_references(compiled,reset_inline_caches);
170 /* Update pointers to words referenced from all code blocks.
171 Only needed after redefining an existing word.
172 If generic words were redefined, inline caches need to be reset. */
173 void factor_vm::update_code_heap_words(bool reset_inline_caches)
175 word_updater updater(this,reset_inline_caches);
176 each_code_block(updater);
179 /* Fix up new words only.
180 Fast path for compilation units that only define new words. */
181 void factor_vm::initialize_code_blocks()
183 std::map<code_block *, cell>::const_iterator iter = code->uninitialized_blocks.begin();
184 std::map<code_block *, cell>::const_iterator end = code->uninitialized_blocks.end();
186 for(; iter != end; iter++)
187 initialize_code_block(iter->first,iter->second);
189 code->uninitialized_blocks.clear();
192 /* Allocates memory */
193 void factor_vm::primitive_modify_code_heap()
195 bool reset_inline_caches = to_boolean(ctx->pop());
196 bool update_existing_words = to_boolean(ctx->pop());
197 data_root<array> alist(ctx->pop(),this);
199 cell count = array_capacity(alist.untagged());
204 for(cell i = 0; i < count; i++)
206 data_root<array> pair(array_nth(alist.untagged(),i),this);
208 data_root<word> word(array_nth(pair.untagged(),0),this);
209 data_root<object> data(array_nth(pair.untagged(),1),this);
214 jit_compile_word(word.value(),data.value(),false);
218 array *compiled_data = data.as<array>().untagged();
219 cell parameters = array_nth(compiled_data,0);
220 cell literals = array_nth(compiled_data,1);
221 cell relocation = array_nth(compiled_data,2);
222 cell labels = array_nth(compiled_data,3);
223 cell code = array_nth(compiled_data,4);
224 cell frame_size = untag_fixnum(array_nth(compiled_data,5));
226 code_block *compiled = add_code_block(
227 code_block_optimized,
236 word->entry_point = compiled->entry_point();
240 critical_error("Expected a quotation or an array",data.value());
245 if(update_existing_words)
246 update_code_heap_words(reset_inline_caches);
248 initialize_code_blocks();
251 code_heap_room factor_vm::code_room()
255 room.size = code->allocator->size;
256 room.occupied_space = code->allocator->occupied_space();
257 room.total_free = code->allocator->free_space();
258 room.contiguous_free = code->allocator->largest_free_block();
259 room.free_block_count = code->allocator->free_block_count();
264 /* Allocates memory */
265 void factor_vm::primitive_code_room()
267 code_heap_room room = code_room();
268 ctx->push(tag<byte_array>(byte_array_from_value(&room)));
271 struct stack_trace_stripper {
272 explicit stack_trace_stripper() {}
274 void operator()(code_block *compiled, cell size)
276 compiled->owner = false_object;
280 void factor_vm::primitive_strip_stack_traces()
282 stack_trace_stripper stripper;
283 each_code_block(stripper);
286 struct code_block_accumulator {
287 std::vector<cell> objects;
289 void operator()(code_block *compiled, cell size)
291 objects.push_back(compiled->owner);
292 objects.push_back(compiled->parameters);
293 objects.push_back(compiled->relocation);
295 objects.push_back(tag_fixnum(compiled->type()));
296 objects.push_back(tag_fixnum(compiled->size()));
298 /* Note: the entry point is always a multiple of the heap
299 alignment (16 bytes). We cannot allocate while iterating
300 through the code heap, so it is not possible to call
301 from_unsigned_cell() here. It is OK, however, to add it as
302 if it were a fixnum, and have library code shift it to the
304 cell entry_point = (cell)compiled->entry_point();
305 FACTOR_ASSERT((entry_point & (data_alignment - 1)) == 0);
306 FACTOR_ASSERT((entry_point & TAG_MASK) == FIXNUM_TYPE);
307 objects.push_back(entry_point);
311 /* Allocates memory */
312 cell factor_vm::code_blocks()
314 code_block_accumulator accum;
315 each_code_block(accum);
316 return std_vector_to_array(accum.objects);
319 /* Allocates memory */
320 void factor_vm::primitive_code_blocks()
322 ctx->push(code_blocks());