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 = 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 void code_heap::set_safepoint_guard(bool locked) {
53 if (!set_memory_locked(safepoint_page, getpagesize(), locked)) {
54 fatal_error("Cannot (un)protect safepoint guard page", safepoint_page);
58 void code_heap::sweep() {
59 auto clear_free_blocks_from_all_blocks = [&](code_block* block, cell size) {
60 std::set<cell>::iterator erase_from =
61 all_blocks.lower_bound((cell)block);
62 std::set<cell>::iterator erase_to =
63 all_blocks.lower_bound((cell)block + size);
64 all_blocks.erase(erase_from, erase_to);
66 allocator->sweep(clear_free_blocks_from_all_blocks);
68 verify_all_blocks_set();
72 void code_heap::verify_all_blocks_set() {
73 auto all_blocks_set_verifier = [&](code_block* block, cell size) {
74 FACTOR_ASSERT(all_blocks.find((cell)block) != all_blocks.end());
76 allocator->iterate(all_blocks_set_verifier);
79 code_block* code_heap::code_block_for_address(cell address) {
80 std::set<cell>::const_iterator blocki = all_blocks.upper_bound(address);
81 FACTOR_ASSERT(blocki != all_blocks.begin());
83 code_block* found_block = (code_block*)*blocki;
84 FACTOR_ASSERT(found_block->entry_point() <=
85 address /* XXX this isn't valid during fixup. should store the
87 && address - found_block->entry_point() <
88 found_block->size()*/);
92 cell code_heap::frame_predecessor(cell frame_top) {
93 cell addr = *(cell*)frame_top;
94 FACTOR_ASSERT(seg->in_segment_p(addr));
95 //FACTOR_ASSERT(addr != 0);
96 code_block* owner = code_block_for_address(addr);
97 cell frame_size = owner->stack_frame_size_for_address(addr);
98 return frame_top + frame_size;
101 /* Recomputes the all_blocks set of code blocks */
102 void code_heap::initialize_all_blocks_set() {
104 auto all_blocks_set_inserter = [&](code_block* block, cell size) {
105 all_blocks.insert((cell)block);
107 allocator->iterate(all_blocks_set_inserter);
109 verify_all_blocks_set();
113 /* Update pointers to words referenced from all code blocks.
114 Only needed after redefining an existing word.
115 If generic words were redefined, inline caches need to be reset. */
116 void factor_vm::update_code_heap_words(bool reset_inline_caches) {
117 auto word_updater = [&](code_block* block, cell size) {
118 update_word_references(block, reset_inline_caches);
120 each_code_block(word_updater);
123 /* Fix up new words only.
124 Fast path for compilation units that only define new words. */
125 void factor_vm::initialize_code_blocks() {
127 FACTOR_FOR_EACH(code->uninitialized_blocks) {
128 initialize_code_block(iter->first, iter->second);
130 code->uninitialized_blocks.clear();
133 /* Allocates memory */
134 void factor_vm::primitive_modify_code_heap() {
135 bool reset_inline_caches = to_boolean(ctx->pop());
136 bool update_existing_words = to_boolean(ctx->pop());
137 data_root<array> alist(ctx->pop(), this);
139 cell count = array_capacity(alist.untagged());
144 for (cell i = 0; i < count; i++) {
145 data_root<array> pair(array_nth(alist.untagged(), i), this);
147 data_root<word> word(array_nth(pair.untagged(), 0), this);
148 data_root<object> data(array_nth(pair.untagged(), 1), this);
150 switch (data.type()) {
152 jit_compile_word(word.value(), data.value(), false);
155 array* compiled_data = data.as<array>().untagged();
156 cell parameters = array_nth(compiled_data, 0);
157 cell literals = array_nth(compiled_data, 1);
158 cell relocation = array_nth(compiled_data, 2);
159 cell labels = array_nth(compiled_data, 3);
160 cell code = array_nth(compiled_data, 4);
161 cell frame_size = untag_fixnum(array_nth(compiled_data, 5));
163 code_block* compiled =
164 add_code_block(code_block_optimized, code, labels, word.value(),
165 relocation, parameters, literals, frame_size);
167 word->entry_point = compiled->entry_point();
170 critical_error("Expected a quotation or an array", data.value());
175 if (update_existing_words)
176 update_code_heap_words(reset_inline_caches);
178 initialize_code_blocks();
181 /* Allocates memory */
182 void factor_vm::primitive_code_room() {
183 allocator_room room = code->allocator->as_allocator_room();
184 ctx->push(tag<byte_array>(byte_array_from_value(&room)));
187 void factor_vm::primitive_strip_stack_traces() {
188 auto stack_trace_stripper = [](code_block* block, cell size) {
189 block->owner = false_object;
191 each_code_block(stack_trace_stripper);
194 /* Allocates memory */
195 void factor_vm::primitive_code_blocks() {
196 std::vector<cell> objects;
197 auto code_block_accumulator = [&](code_block* block, cell size) {
198 objects.push_back(block->owner);
199 objects.push_back(block->parameters);
200 objects.push_back(block->relocation);
202 objects.push_back(tag_fixnum(block->type()));
203 objects.push_back(tag_fixnum(block->size()));
205 /* Note: the entry point is always a multiple of the heap
206 alignment (16 bytes). We cannot allocate while iterating
207 through the code heap, so it is not possible to call
208 from_unsigned_cell() here. It is OK, however, to add it as
209 if it were a fixnum, and have library code shift it to the
211 cell entry_point = block->entry_point();
212 FACTOR_ASSERT((entry_point & (data_alignment - 1)) == 0);
213 FACTOR_ASSERT((entry_point & TAG_MASK) == FIXNUM_TYPE);
214 objects.push_back(entry_point);
216 each_code_block(code_block_accumulator);
217 ctx->push(std_vector_to_array(objects));