]> gitweb.factorcode.org Git - factor.git/blob - vm/code_heap.cpp
scryfall: make decks better, import from moxfield
[factor.git] / vm / code_heap.cpp
1 #include "master.hpp"
2
3 namespace factor {
4
5 code_heap::code_heap(cell size) {
6   if (size > ((uint64_t)1 << (sizeof(cell) * 8 - 5)))
7     fatal_error("Heap too large", size);
8   seg = new segment(align_page(size), true);
9   if (!seg)
10     fatal_error("Out of memory in code_heap constructor", size);
11
12   cell start = seg->start + getpagesize() + seh_area_size;
13
14   allocator = new free_list_allocator<code_block>(seg->end - start, start);
15
16   // See os-windows-x86.64.cpp for seh_area usage
17   safepoint_page = seg->start;
18   seh_area = (char*)seg->start + getpagesize();
19 }
20
21 code_heap::~code_heap() {
22   delete allocator;
23   allocator = NULL;
24   delete seg;
25   seg = NULL;
26 }
27
28 void code_heap::write_barrier(code_block* compiled) {
29   points_to_nursery.insert(compiled);
30   points_to_aging.insert(compiled);
31 }
32
33 void code_heap::clear_remembered_set() {
34   points_to_nursery.clear();
35   points_to_aging.clear();
36 }
37
38 bool code_heap::uninitialized_p(code_block* compiled) {
39   return uninitialized_blocks.count(compiled) > 0;
40 }
41
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);
48 }
49
50 void code_heap::flush_icache() { factor::flush_icache(seg->start, seg->size); }
51
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);
55   }
56 }
57
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);
65   };
66   allocator->sweep(clear_free_blocks_from_all_blocks);
67 #ifdef FACTOR_DEBUG
68   verify_all_blocks_set();
69 #endif
70 }
71
72 void code_heap::verify_all_blocks_set() {
73   auto all_blocks_set_verifier = [&](code_block* block, cell size) {
74     (void)block;
75     (void)size;
76     FACTOR_ASSERT(all_blocks.find((cell)block) != all_blocks.end());
77   };
78   allocator->iterate(all_blocks_set_verifier, no_fixup());
79 }
80
81 code_block* code_heap::code_block_for_address(cell address) {
82   std::set<cell>::const_iterator blocki = all_blocks.upper_bound(address);
83   FACTOR_ASSERT(blocki != all_blocks.begin());
84   --blocki;
85   code_block* found_block = (code_block*)*blocki;
86   FACTOR_ASSERT(found_block->entry_point() <=
87                 address // XXX this isn't valid during fixup. should store the
88                         //     size in the map
89                         //    && address - found_block->entry_point() <
90                         //       found_block->size()
91                 );
92   return found_block;
93 }
94
95 cell code_heap::frame_predecessor(cell frame_top) {
96   cell addr = *(cell*)frame_top;
97   FACTOR_ASSERT(seg->in_segment_p(addr));
98   code_block* owner = code_block_for_address(addr);
99   cell frame_size = owner->stack_frame_size_for_address(addr);
100   return frame_top + frame_size;
101 }
102
103 // Recomputes the all_blocks set of code blocks
104 void code_heap::initialize_all_blocks_set() {
105   all_blocks.clear();
106   auto all_blocks_set_inserter = [&](code_block* block, cell size) {
107     (void)size;
108     all_blocks.insert((cell)block);
109   };
110   allocator->iterate(all_blocks_set_inserter, no_fixup());
111 #ifdef FACTOR_DEBUG
112   verify_all_blocks_set();
113 #endif
114 }
115
116 // Update pointers to words referenced from all code blocks.
117 // Only needed after redefining an existing word.
118 // If generic words were redefined, inline caches need to be reset.
119 void factor_vm::update_code_heap_words(bool reset_inline_caches) {
120   auto word_updater = [&](code_block* block, cell size) {
121     (void)size;
122     update_word_references(block, reset_inline_caches);
123   };
124   each_code_block(word_updater);
125 }
126
127 // Allocates memory
128 void factor_vm::primitive_modify_code_heap() {
129   bool reset_inline_caches = to_boolean(ctx->pop());
130   bool update_existing_words = to_boolean(ctx->pop());
131   data_root<array> alist(ctx->pop(), this);
132
133   cell count = array_capacity(alist.untagged());
134
135   if (count == 0)
136     return;
137
138   for (cell i = 0; i < count; i++) {
139     data_root<array> pair(array_nth(alist.untagged(), i), this);
140
141     data_root<word> word(array_nth(pair.untagged(), 0), this);
142     data_root<object> data(array_nth(pair.untagged(), 1), this);
143
144     switch (data.type()) {
145       case QUOTATION_TYPE:
146       case TUPLE_TYPE: // for curry/compose, see issue #2763
147         jit_compile_word(word.value(), data.value(), false);
148         break;
149       case ARRAY_TYPE: {
150         array* compiled_data = data.as<array>().untagged();
151         cell parameters = array_nth(compiled_data, 0);
152         cell literals = array_nth(compiled_data, 1);
153         cell relocation = array_nth(compiled_data, 2);
154         cell labels = array_nth(compiled_data, 3);
155         cell code = array_nth(compiled_data, 4);
156         cell frame_size = untag_fixnum(array_nth(compiled_data, 5));
157
158         code_block* compiled =
159             add_code_block(CODE_BLOCK_OPTIMIZED, code, labels, word.value(),
160                            relocation, parameters, literals, frame_size);
161
162         word->entry_point = compiled->entry_point();
163       } break;
164       default:
165         critical_error("Expected a quotation or an array", data.value());
166         break;
167     }
168   }
169
170   if (update_existing_words)
171     update_code_heap_words(reset_inline_caches);
172   else {
173     // Fast path for compilation units that only define new words.
174     FACTOR_FOR_EACH(code->uninitialized_blocks) {
175       initialize_code_block(iter->first, iter->second);
176     }
177     code->uninitialized_blocks.clear();
178   }
179   FACTOR_ASSERT(code->uninitialized_blocks.size() == 0);
180 }
181
182 // Allocates memory
183 void factor_vm::primitive_code_room() {
184   allocator_room room = code->allocator->as_allocator_room();
185   ctx->push(tag<byte_array>(byte_array_from_value(&room)));
186 }
187
188 void factor_vm::primitive_strip_stack_traces() {
189   auto stack_trace_stripper = [](code_block* block, cell size) {
190     (void)size;
191     block->owner = false_object;
192   };
193   each_code_block(stack_trace_stripper);
194 }
195
196 // Allocates memory
197 void factor_vm::primitive_code_blocks() {
198   std::vector<cell> objects;
199   auto code_block_accumulator = [&](code_block* block, cell size) {
200     (void)size;
201     objects.push_back(block->owner);
202     objects.push_back(block->parameters);
203     objects.push_back(block->relocation);
204
205     objects.push_back(tag_fixnum(block->type()));
206     objects.push_back(tag_fixnum(block->size()));
207
208     // Note: the entry point is always a multiple of the heap
209     // alignment (16 bytes). We cannot allocate while iterating
210     // through the code heap, so it is not possible to call
211     // from_unsigned_cell() here. It is OK, however, to add it as
212     // if it were a fixnum, and have library code shift it to the
213     // left by 4.
214     cell entry_point = block->entry_point();
215     FACTOR_ASSERT((entry_point & (data_alignment - 1)) == 0);
216     FACTOR_ASSERT((entry_point & TAG_MASK) == FIXNUM_TYPE);
217     objects.push_back(entry_point);
218   };
219   each_code_block(code_block_accumulator);
220   ctx->push(std_vector_to_array(objects));
221 }
222
223 }