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 void code_heap::free(code_block* compiled) {
43 FACTOR_ASSERT(!uninitialized_p(compiled));
44 points_to_nursery.erase(compiled);
45 points_to_aging.erase(compiled);
46 all_blocks.erase((cell)compiled);
47 allocator->free(compiled);
50 void code_heap::flush_icache() { factor::flush_icache(seg->start, seg->size); }
52 struct clear_free_blocks_from_all_blocks_iterator {
55 clear_free_blocks_from_all_blocks_iterator(code_heap* code) : code(code) {}
57 void operator()(code_block* free_block, cell size) {
58 std::set<cell>::iterator erase_from =
59 code->all_blocks.lower_bound((cell)free_block);
60 std::set<cell>::iterator erase_to =
61 code->all_blocks.lower_bound((cell)free_block + size);
63 code->all_blocks.erase(erase_from, erase_to);
67 void code_heap::sweep() {
68 clear_free_blocks_from_all_blocks_iterator clearer(this);
69 allocator->sweep(clearer);
71 verify_all_blocks_set();
75 struct all_blocks_set_verifier {
76 std::set<cell>* all_blocks;
78 all_blocks_set_verifier(std::set<cell>* all_blocks)
79 : all_blocks(all_blocks) {}
81 void operator()(code_block* block, cell size) {
82 FACTOR_ASSERT(all_blocks->find((cell)block) != all_blocks->end());
86 void code_heap::verify_all_blocks_set() {
87 all_blocks_set_verifier verifier(&all_blocks);
88 allocator->iterate(verifier);
91 code_block* code_heap::code_block_for_address(cell address) {
92 std::set<cell>::const_iterator blocki = all_blocks.upper_bound(address);
93 FACTOR_ASSERT(blocki != all_blocks.begin());
95 code_block* found_block = (code_block*)*blocki;
96 FACTOR_ASSERT(found_block->entry_point() <=
97 address /* XXX this isn't valid during fixup. should store the
99 && address - found_block->entry_point() <
100 found_block->size()*/);
104 struct all_blocks_set_inserter {
107 all_blocks_set_inserter(code_heap* code) : code(code) {}
109 void operator()(code_block* block, cell size) {
110 code->all_blocks.insert((cell)block);
114 void code_heap::initialize_all_blocks_set() {
116 all_blocks_set_inserter inserter(this);
117 allocator->iterate(inserter);
119 verify_all_blocks_set();
123 /* Allocate a code heap during startup */
124 void factor_vm::init_code_heap(cell size) { code = new code_heap(size); }
126 struct word_updater {
128 bool reset_inline_caches;
130 word_updater(factor_vm* parent, bool reset_inline_caches)
131 : parent(parent), reset_inline_caches(reset_inline_caches) {}
133 void operator()(code_block* compiled, cell size) {
134 parent->update_word_references(compiled, reset_inline_caches);
138 /* Update pointers to words referenced from all code blocks.
139 Only needed after redefining an existing word.
140 If generic words were redefined, inline caches need to be reset. */
141 void factor_vm::update_code_heap_words(bool reset_inline_caches) {
142 word_updater updater(this, reset_inline_caches);
143 each_code_block(updater);
146 /* Fix up new words only.
147 Fast path for compilation units that only define new words. */
148 void factor_vm::initialize_code_blocks() {
150 FACTOR_FOR_EACH(code->uninitialized_blocks) {
151 initialize_code_block(iter->first, iter->second);
153 code->uninitialized_blocks.clear();
156 /* Allocates memory */
157 void factor_vm::primitive_modify_code_heap() {
158 bool reset_inline_caches = to_boolean(ctx->pop());
159 bool update_existing_words = to_boolean(ctx->pop());
160 data_root<array> alist(ctx->pop(), this);
162 cell count = array_capacity(alist.untagged());
167 for (cell i = 0; i < count; i++) {
168 data_root<array> pair(array_nth(alist.untagged(), i), this);
170 data_root<word> word(array_nth(pair.untagged(), 0), this);
171 data_root<object> data(array_nth(pair.untagged(), 1), this);
173 switch (data.type()) {
175 jit_compile_word(word.value(), data.value(), false);
178 array* compiled_data = data.as<array>().untagged();
179 cell parameters = array_nth(compiled_data, 0);
180 cell literals = array_nth(compiled_data, 1);
181 cell relocation = array_nth(compiled_data, 2);
182 cell labels = array_nth(compiled_data, 3);
183 cell code = array_nth(compiled_data, 4);
184 cell frame_size = untag_fixnum(array_nth(compiled_data, 5));
186 code_block* compiled =
187 add_code_block(code_block_optimized, code, labels, word.value(),
188 relocation, parameters, literals, frame_size);
190 word->entry_point = compiled->entry_point();
193 critical_error("Expected a quotation or an array", data.value());
198 if (update_existing_words)
199 update_code_heap_words(reset_inline_caches);
201 initialize_code_blocks();
204 /* Allocates memory */
205 void factor_vm::primitive_code_room() {
206 allocator_room room = code->allocator->as_allocator_room();
207 ctx->push(tag<byte_array>(byte_array_from_value(&room)));
210 struct stack_trace_stripper {
211 stack_trace_stripper() {}
213 void operator()(code_block* compiled, cell size) {
214 compiled->owner = false_object;
218 void factor_vm::primitive_strip_stack_traces() {
219 stack_trace_stripper stripper;
220 each_code_block(stripper);
223 struct code_block_accumulator {
224 std::vector<cell> objects;
226 void operator()(code_block* compiled, cell size) {
227 objects.push_back(compiled->owner);
228 objects.push_back(compiled->parameters);
229 objects.push_back(compiled->relocation);
231 objects.push_back(tag_fixnum(compiled->type()));
232 objects.push_back(tag_fixnum(compiled->size()));
234 /* Note: the entry point is always a multiple of the heap
235 alignment (16 bytes). We cannot allocate while iterating
236 through the code heap, so it is not possible to call
237 from_unsigned_cell() here. It is OK, however, to add it as
238 if it were a fixnum, and have library code shift it to the
240 cell entry_point = compiled->entry_point();
241 FACTOR_ASSERT((entry_point & (data_alignment - 1)) == 0);
242 FACTOR_ASSERT((entry_point & TAG_MASK) == FIXNUM_TYPE);
243 objects.push_back(entry_point);
247 /* Allocates memory */
248 cell factor_vm::code_blocks() {
249 code_block_accumulator accum;
250 each_code_block(accum);
251 return std_vector_to_array(accum.objects);
254 /* Allocates memory */
255 void factor_vm::primitive_code_blocks() { ctx->push(code_blocks()); }