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 assert(!uninitialized_p(compiled));
64 points_to_nursery.erase(compiled);
65 points_to_aging.erase(compiled);
66 allocator->free(compiled);
69 void code_heap::flush_icache()
71 factor::flush_icache(seg->start,seg->size);
74 struct address_finder {
76 code_block *found_code_block;
78 address_finder(cell address)
79 : address(address), found_code_block(NULL) {}
81 void operator()(code_block *block, cell size)
83 if ((cell)block->entry_point() <= address
84 && address - (cell)block->entry_point() < block->size())
86 assert(found_code_block == NULL);
87 found_code_block = block;
92 code_block *code_heap::code_block_for_address(cell address)
94 address_finder finder(address);
95 allocator->iterate(finder);
96 return finder.found_code_block;
99 /* Allocate a code heap during startup */
100 void factor_vm::init_code_heap(cell size)
102 code = new code_heap(size);
105 struct word_updater {
107 bool reset_inline_caches;
109 word_updater(factor_vm *parent_, bool reset_inline_caches_) :
110 parent(parent_), reset_inline_caches(reset_inline_caches_) {}
112 void operator()(code_block *compiled, cell size)
114 parent->update_word_references(compiled,reset_inline_caches);
118 /* Update pointers to words referenced from all code blocks.
119 Only needed after redefining an existing word.
120 If generic words were redefined, inline caches need to be reset. */
121 void factor_vm::update_code_heap_words(bool reset_inline_caches)
123 word_updater updater(this,reset_inline_caches);
124 each_code_block(updater);
127 /* Fix up new words only.
128 Fast path for compilation units that only define new words. */
129 void factor_vm::initialize_code_blocks()
131 std::map<code_block *, cell>::const_iterator iter = code->uninitialized_blocks.begin();
132 std::map<code_block *, cell>::const_iterator end = code->uninitialized_blocks.end();
134 for(; iter != end; iter++)
135 initialize_code_block(iter->first,iter->second);
137 code->uninitialized_blocks.clear();
140 void factor_vm::primitive_modify_code_heap()
142 bool reset_inline_caches = to_boolean(ctx->pop());
143 bool update_existing_words = to_boolean(ctx->pop());
144 data_root<array> alist(ctx->pop(),this);
146 cell count = array_capacity(alist.untagged());
151 for(cell i = 0; i < count; i++)
153 data_root<array> pair(array_nth(alist.untagged(),i),this);
155 data_root<word> word(array_nth(pair.untagged(),0),this);
156 data_root<object> data(array_nth(pair.untagged(),1),this);
161 jit_compile_word(word.value(),data.value(),false);
165 array *compiled_data = data.as<array>().untagged();
166 cell parameters = array_nth(compiled_data,0);
167 cell literals = array_nth(compiled_data,1);
168 cell relocation = array_nth(compiled_data,2);
169 cell labels = array_nth(compiled_data,3);
170 cell code = array_nth(compiled_data,4);
172 code_block *compiled = add_code_block(
173 code_block_optimized,
181 word->code = compiled;
185 critical_error("Expected a quotation or an array",data.value());
189 update_word_entry_point(word.untagged());
192 if(update_existing_words)
193 update_code_heap_words(reset_inline_caches);
195 initialize_code_blocks();
198 code_heap_room factor_vm::code_room()
202 room.size = code->allocator->size;
203 room.occupied_space = code->allocator->occupied_space();
204 room.total_free = code->allocator->free_space();
205 room.contiguous_free = code->allocator->largest_free_block();
206 room.free_block_count = code->allocator->free_block_count();
211 void factor_vm::primitive_code_room()
213 code_heap_room room = code_room();
214 ctx->push(tag<byte_array>(byte_array_from_value(&room)));
217 struct stack_trace_stripper {
218 explicit stack_trace_stripper() {}
220 void operator()(code_block *compiled, cell size)
222 compiled->owner = false_object;
226 void factor_vm::primitive_strip_stack_traces()
228 stack_trace_stripper stripper;
229 each_code_block(stripper);
232 struct code_block_accumulator {
233 std::vector<cell> objects;
235 void operator()(code_block *compiled, cell size)
237 objects.push_back(compiled->owner);
238 objects.push_back(compiled->parameters);
239 objects.push_back(compiled->relocation);
241 objects.push_back(tag_fixnum(compiled->type()));
242 objects.push_back(tag_fixnum(compiled->size()));
244 /* Note: the entry point is always a multiple of the heap
245 alignment (16 bytes). We cannot allocate while iterating
246 through the code heap, so it is not possible to call
247 from_unsigned_cell() here. It is OK, however, to add it as
248 if it were a fixnum, and have library code shift it to the
250 cell entry_point = (cell)compiled->entry_point();
251 assert((entry_point & (data_alignment - 1)) == 0);
252 assert((entry_point & TAG_MASK) == FIXNUM_TYPE);
253 objects.push_back(entry_point);
257 cell factor_vm::code_blocks()
259 code_block_accumulator accum;
260 each_code_block(accum);
261 return std_vector_to_array(accum.objects);
264 void factor_vm::primitive_code_blocks()
266 ctx->push(code_blocks());