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 + 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 seh_area = (char *)seg->start;
20 code_heap::~code_heap()
28 void code_heap::write_barrier(code_block *compiled)
30 points_to_nursery.insert(compiled);
31 points_to_aging.insert(compiled);
34 void code_heap::clear_remembered_set()
36 points_to_nursery.clear();
37 points_to_aging.clear();
40 bool code_heap::uninitialized_p(code_block *compiled)
42 return uninitialized_blocks.count(compiled) > 0;
45 bool code_heap::marked_p(code_block *compiled)
47 return allocator->state.marked_p(compiled);
50 void code_heap::set_marked_p(code_block *compiled)
52 allocator->state.set_marked_p(compiled);
55 void code_heap::clear_mark_bits()
57 allocator->state.clear_mark_bits();
60 void code_heap::free(code_block *compiled)
62 assert(!uninitialized_p(compiled));
63 points_to_nursery.erase(compiled);
64 points_to_aging.erase(compiled);
65 allocator->free(compiled);
68 void code_heap::flush_icache()
70 factor::flush_icache(seg->start,seg->size);
73 /* Allocate a code heap during startup */
74 void factor_vm::init_code_heap(cell size)
76 code = new code_heap(size);
79 bool factor_vm::in_code_heap_p(cell ptr)
81 return (ptr >= code->seg->start && ptr <= code->seg->end);
86 bool reset_inline_caches;
88 word_updater(factor_vm *parent_, bool reset_inline_caches_) :
89 parent(parent_), reset_inline_caches(reset_inline_caches_) {}
91 void operator()(code_block *compiled, cell size)
93 parent->update_word_references(compiled,reset_inline_caches);
97 /* Update pointers to words referenced from all code blocks.
98 Only needed after redefining an existing word.
99 If generic words were redefined, inline caches need to be reset. */
100 void factor_vm::update_code_heap_words(bool reset_inline_caches)
102 word_updater updater(this,reset_inline_caches);
103 each_code_block(updater);
106 /* Fix up new words only.
107 Fast path for compilation units that only define new words. */
108 void factor_vm::initialize_code_blocks()
110 std::map<code_block *, cell>::const_iterator iter = code->uninitialized_blocks.begin();
111 std::map<code_block *, cell>::const_iterator end = code->uninitialized_blocks.end();
113 for(; iter != end; iter++)
114 initialize_code_block(iter->first,iter->second);
116 code->uninitialized_blocks.clear();
119 void factor_vm::primitive_modify_code_heap()
121 bool reset_inline_caches = to_boolean(ctx->pop());
122 bool update_existing_words = to_boolean(ctx->pop());
123 data_root<array> alist(ctx->pop(),this);
125 cell count = array_capacity(alist.untagged());
130 for(cell i = 0; i < count; i++)
132 data_root<array> pair(array_nth(alist.untagged(),i),this);
134 data_root<word> word(array_nth(pair.untagged(),0),this);
135 data_root<object> data(array_nth(pair.untagged(),1),this);
140 jit_compile_word(word.value(),data.value(),false);
144 array *compiled_data = data.as<array>().untagged();
145 cell parameters = array_nth(compiled_data,0);
146 cell literals = array_nth(compiled_data,1);
147 cell relocation = array_nth(compiled_data,2);
148 cell labels = array_nth(compiled_data,3);
149 cell code = array_nth(compiled_data,4);
151 code_block *compiled = add_code_block(
152 code_block_optimized,
160 word->code = compiled;
164 critical_error("Expected a quotation or an array",data.value());
168 update_word_entry_point(word.untagged());
171 if(update_existing_words)
172 update_code_heap_words(reset_inline_caches);
174 initialize_code_blocks();
177 code_heap_room factor_vm::code_room()
181 room.size = code->allocator->size;
182 room.occupied_space = code->allocator->occupied_space();
183 room.total_free = code->allocator->free_space();
184 room.contiguous_free = code->allocator->largest_free_block();
185 room.free_block_count = code->allocator->free_block_count();
190 void factor_vm::primitive_code_room()
192 code_heap_room room = code_room();
193 ctx->push(tag<byte_array>(byte_array_from_value(&room)));
196 struct stack_trace_stripper {
197 explicit stack_trace_stripper() {}
199 void operator()(code_block *compiled, cell size)
201 compiled->owner = false_object;
205 void factor_vm::primitive_strip_stack_traces()
207 stack_trace_stripper stripper;
208 each_code_block(stripper);
211 struct code_block_accumulator {
212 std::vector<cell> objects;
214 void operator()(code_block *compiled, cell size)
216 objects.push_back(compiled->owner);
217 objects.push_back(compiled->parameters);
218 objects.push_back(compiled->relocation);
220 objects.push_back(tag_fixnum(compiled->type()));
221 objects.push_back(tag_fixnum(compiled->size()));
223 /* Note: the entry point is always a multiple of the heap
224 alignment (16 bytes). We cannot allocate while iterating
225 through the code heap, so it is not possible to call
226 from_unsigned_cell() here. It is OK, however, to add it as
227 if it were a fixnum, and have library code shift it to the
229 cell entry_point = (cell)compiled->entry_point();
230 assert((entry_point & (data_alignment - 1)) == 0);
231 assert((entry_point & TAG_MASK) == FIXNUM_TYPE);
232 objects.push_back(entry_point);
236 cell factor_vm::code_blocks()
238 code_block_accumulator accum;
239 each_code_block(accum);
240 return std_vector_to_array(accum.objects);
243 void factor_vm::primitive_code_blocks()
245 ctx->push(code_blocks());