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);
10 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() {
28 void code_heap::write_barrier(code_block* compiled) {
29 points_to_nursery.insert(compiled);
30 points_to_aging.insert(compiled);
33 void code_heap::clear_remembered_set() {
34 points_to_nursery.clear();
35 points_to_aging.clear();
38 bool code_heap::uninitialized_p(code_block* compiled) {
39 return uninitialized_blocks.count(compiled) > 0;
42 bool code_heap::marked_p(code_block* compiled) {
43 return allocator->state.marked_p(compiled);
46 void code_heap::set_marked_p(code_block* compiled) {
47 allocator->state.set_marked_p(compiled);
50 void code_heap::clear_mark_bits() { allocator->state.clear_mark_bits(); }
52 void code_heap::free(code_block* compiled) {
53 FACTOR_ASSERT(!uninitialized_p(compiled));
54 points_to_nursery.erase(compiled);
55 points_to_aging.erase(compiled);
56 all_blocks.erase((cell) compiled);
57 allocator->free(compiled);
60 void code_heap::flush_icache() { factor::flush_icache(seg->start, seg->size); }
62 struct clear_free_blocks_from_all_blocks_iterator {
65 clear_free_blocks_from_all_blocks_iterator(code_heap* code) : code(code) {}
67 void operator()(code_block* free_block, cell size) {
68 std::set<cell>::iterator erase_from =
69 code->all_blocks.lower_bound((cell) free_block);
70 std::set<cell>::iterator erase_to =
71 code->all_blocks.lower_bound((cell) free_block + size);
73 code->all_blocks.erase(erase_from, erase_to);
77 void code_heap::sweep() {
78 clear_free_blocks_from_all_blocks_iterator clearer(this);
79 allocator->sweep(clearer);
81 verify_all_blocks_set();
85 struct all_blocks_set_verifier {
86 std::set<cell>* all_blocks;
88 all_blocks_set_verifier(std::set<cell>* all_blocks)
89 : all_blocks(all_blocks) {}
91 void operator()(code_block* block, cell size) {
92 FACTOR_ASSERT(all_blocks->find((cell) block) != all_blocks->end());
96 void code_heap::verify_all_blocks_set() {
97 all_blocks_set_verifier verifier(&all_blocks);
98 allocator->iterate(verifier);
101 code_block* code_heap::code_block_for_address(cell address) {
102 std::set<cell>::const_iterator blocki = all_blocks.upper_bound(address);
103 FACTOR_ASSERT(blocki != all_blocks.begin());
105 code_block* found_block = (code_block*)*blocki;
106 FACTOR_ASSERT((cell) found_block->entry_point() <=
107 address /* XXX this isn't valid during fixup. should store the
109 && address - (cell)found_block->entry_point() <
110 found_block->size()*/);
114 struct all_blocks_set_inserter {
117 all_blocks_set_inserter(code_heap* code) : code(code) {}
119 void operator()(code_block* block, cell size) {
120 code->all_blocks.insert((cell) block);
124 void code_heap::initialize_all_blocks_set() {
126 all_blocks_set_inserter inserter(this);
127 allocator->iterate(inserter);
129 verify_all_blocks_set();
133 /* Allocate a code heap during startup */
134 void factor_vm::init_code_heap(cell size) { code = new code_heap(size); }
136 struct word_updater {
138 bool reset_inline_caches;
140 word_updater(factor_vm* parent, bool reset_inline_caches)
141 : parent(parent), reset_inline_caches(reset_inline_caches) {}
143 void operator()(code_block* compiled, cell size) {
144 parent->update_word_references(compiled, reset_inline_caches);
148 /* Update pointers to words referenced from all code blocks.
149 Only needed after redefining an existing word.
150 If generic words were redefined, inline caches need to be reset. */
151 void factor_vm::update_code_heap_words(bool reset_inline_caches) {
152 word_updater updater(this, reset_inline_caches);
153 each_code_block(updater);
156 /* Fix up new words only.
157 Fast path for compilation units that only define new words. */
158 void factor_vm::initialize_code_blocks() {
159 std::map<code_block*, cell>::const_iterator iter =
160 code->uninitialized_blocks.begin();
161 std::map<code_block*, cell>::const_iterator end =
162 code->uninitialized_blocks.end();
164 for (; iter != end; iter++)
165 initialize_code_block(iter->first, iter->second);
167 code->uninitialized_blocks.clear();
170 /* Allocates memory */
171 void factor_vm::primitive_modify_code_heap() {
172 bool reset_inline_caches = to_boolean(ctx->pop());
173 bool update_existing_words = to_boolean(ctx->pop());
174 data_root<array> alist(ctx->pop(), this);
176 cell count = array_capacity(alist.untagged());
181 for (cell i = 0; i < count; i++) {
182 data_root<array> pair(array_nth(alist.untagged(), i), this);
184 data_root<word> word(array_nth(pair.untagged(), 0), this);
185 data_root<object> data(array_nth(pair.untagged(), 1), this);
187 switch (data.type()) {
189 jit_compile_word(word.value(), data.value(), false);
192 array* compiled_data = data.as<array>().untagged();
193 cell parameters = array_nth(compiled_data, 0);
194 cell literals = array_nth(compiled_data, 1);
195 cell relocation = array_nth(compiled_data, 2);
196 cell labels = array_nth(compiled_data, 3);
197 cell code = array_nth(compiled_data, 4);
198 cell frame_size = untag_fixnum(array_nth(compiled_data, 5));
200 code_block* compiled =
201 add_code_block(code_block_optimized, code, labels, word.value(),
202 relocation, parameters, literals, frame_size);
204 word->entry_point = compiled->entry_point();
207 critical_error("Expected a quotation or an array", data.value());
212 if (update_existing_words)
213 update_code_heap_words(reset_inline_caches);
215 initialize_code_blocks();
218 code_heap_room factor_vm::code_room() {
221 room.size = code->allocator->size;
222 room.occupied_space = code->allocator->occupied_space();
223 room.total_free = code->allocator->free_space();
224 room.contiguous_free = code->allocator->largest_free_block();
225 room.free_block_count = code->allocator->free_block_count();
230 /* Allocates memory */
231 void factor_vm::primitive_code_room() {
232 code_heap_room room = code_room();
233 ctx->push(tag<byte_array>(byte_array_from_value(&room)));
236 struct stack_trace_stripper {
237 stack_trace_stripper() {}
239 void operator()(code_block* compiled, cell size) {
240 compiled->owner = false_object;
244 void factor_vm::primitive_strip_stack_traces() {
245 stack_trace_stripper stripper;
246 each_code_block(stripper);
249 struct code_block_accumulator {
250 std::vector<cell> objects;
252 void operator()(code_block* compiled, cell size) {
253 objects.push_back(compiled->owner);
254 objects.push_back(compiled->parameters);
255 objects.push_back(compiled->relocation);
257 objects.push_back(tag_fixnum(compiled->type()));
258 objects.push_back(tag_fixnum(compiled->size()));
260 /* Note: the entry point is always a multiple of the heap
261 alignment (16 bytes). We cannot allocate while iterating
262 through the code heap, so it is not possible to call
263 from_unsigned_cell() here. It is OK, however, to add it as
264 if it were a fixnum, and have library code shift it to the
266 cell entry_point = (cell) compiled->entry_point();
267 FACTOR_ASSERT((entry_point & (data_alignment - 1)) == 0);
268 FACTOR_ASSERT((entry_point & TAG_MASK) == FIXNUM_TYPE);
269 objects.push_back(entry_point);
273 /* Allocates memory */
274 cell factor_vm::code_blocks() {
275 code_block_accumulator accum;
276 each_code_block(accum);
277 return std_vector_to_array(accum.objects);
280 /* Allocates memory */
281 void factor_vm::primitive_code_blocks() { ctx->push(code_blocks()); }