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((cell)compiled);
46 void code_heap::set_marked_p(code_block* compiled) {
47 allocator->state.set_marked_p((cell)compiled, compiled->size());
50 void code_heap::free(code_block* compiled) {
51 FACTOR_ASSERT(!uninitialized_p(compiled));
52 points_to_nursery.erase(compiled);
53 points_to_aging.erase(compiled);
54 all_blocks.erase((cell)compiled);
55 allocator->free(compiled);
58 void code_heap::flush_icache() { factor::flush_icache(seg->start, seg->size); }
60 struct clear_free_blocks_from_all_blocks_iterator {
63 clear_free_blocks_from_all_blocks_iterator(code_heap* code) : code(code) {}
65 void operator()(code_block* free_block, cell size) {
66 std::set<cell>::iterator erase_from =
67 code->all_blocks.lower_bound((cell)free_block);
68 std::set<cell>::iterator erase_to =
69 code->all_blocks.lower_bound((cell)free_block + size);
71 code->all_blocks.erase(erase_from, erase_to);
75 void code_heap::sweep() {
76 clear_free_blocks_from_all_blocks_iterator clearer(this);
77 allocator->sweep(clearer);
79 verify_all_blocks_set();
83 struct all_blocks_set_verifier {
84 std::set<cell>* all_blocks;
86 all_blocks_set_verifier(std::set<cell>* all_blocks)
87 : all_blocks(all_blocks) {}
89 void operator()(code_block* block, cell size) {
90 FACTOR_ASSERT(all_blocks->find((cell)block) != all_blocks->end());
94 void code_heap::verify_all_blocks_set() {
95 all_blocks_set_verifier verifier(&all_blocks);
96 allocator->iterate(verifier);
99 code_block* code_heap::code_block_for_address(cell address) {
100 std::set<cell>::const_iterator blocki = all_blocks.upper_bound(address);
101 FACTOR_ASSERT(blocki != all_blocks.begin());
103 code_block* found_block = (code_block*)*blocki;
104 FACTOR_ASSERT(found_block->entry_point() <=
105 address /* XXX this isn't valid during fixup. should store the
107 && address - found_block->entry_point() <
108 found_block->size()*/);
112 struct all_blocks_set_inserter {
115 all_blocks_set_inserter(code_heap* code) : code(code) {}
117 void operator()(code_block* block, cell size) {
118 code->all_blocks.insert((cell)block);
122 void code_heap::initialize_all_blocks_set() {
124 all_blocks_set_inserter inserter(this);
125 allocator->iterate(inserter);
127 verify_all_blocks_set();
131 /* Allocate a code heap during startup */
132 void factor_vm::init_code_heap(cell size) { code = new code_heap(size); }
134 struct word_updater {
136 bool reset_inline_caches;
138 word_updater(factor_vm* parent, bool reset_inline_caches)
139 : parent(parent), reset_inline_caches(reset_inline_caches) {}
141 void operator()(code_block* compiled, cell size) {
142 parent->update_word_references(compiled, reset_inline_caches);
146 /* Update pointers to words referenced from all code blocks.
147 Only needed after redefining an existing word.
148 If generic words were redefined, inline caches need to be reset. */
149 void factor_vm::update_code_heap_words(bool reset_inline_caches) {
150 word_updater updater(this, reset_inline_caches);
151 each_code_block(updater);
154 /* Fix up new words only.
155 Fast path for compilation units that only define new words. */
156 void factor_vm::initialize_code_blocks() {
157 std::map<code_block*, cell>::const_iterator iter =
158 code->uninitialized_blocks.begin();
159 std::map<code_block*, cell>::const_iterator end =
160 code->uninitialized_blocks.end();
162 for (; iter != end; iter++)
163 initialize_code_block(iter->first, iter->second);
165 code->uninitialized_blocks.clear();
168 /* Allocates memory */
169 void factor_vm::primitive_modify_code_heap() {
170 bool reset_inline_caches = to_boolean(ctx->pop());
171 bool update_existing_words = to_boolean(ctx->pop());
172 data_root<array> alist(ctx->pop(), this);
174 cell count = array_capacity(alist.untagged());
179 for (cell i = 0; i < count; i++) {
180 data_root<array> pair(array_nth(alist.untagged(), i), this);
182 data_root<word> word(array_nth(pair.untagged(), 0), this);
183 data_root<object> data(array_nth(pair.untagged(), 1), this);
185 switch (data.type()) {
187 jit_compile_word(word.value(), data.value(), false);
190 array* compiled_data = data.as<array>().untagged();
191 cell parameters = array_nth(compiled_data, 0);
192 cell literals = array_nth(compiled_data, 1);
193 cell relocation = array_nth(compiled_data, 2);
194 cell labels = array_nth(compiled_data, 3);
195 cell code = array_nth(compiled_data, 4);
196 cell frame_size = untag_fixnum(array_nth(compiled_data, 5));
198 code_block* compiled =
199 add_code_block(code_block_optimized, code, labels, word.value(),
200 relocation, parameters, literals, frame_size);
202 word->entry_point = compiled->entry_point();
205 critical_error("Expected a quotation or an array", data.value());
210 if (update_existing_words)
211 update_code_heap_words(reset_inline_caches);
213 initialize_code_blocks();
216 /* Allocates memory */
217 void factor_vm::primitive_code_room() {
218 allocator_room room = code->allocator->as_allocator_room();
219 ctx->push(tag<byte_array>(byte_array_from_value(&room)));
222 struct stack_trace_stripper {
223 stack_trace_stripper() {}
225 void operator()(code_block* compiled, cell size) {
226 compiled->owner = false_object;
230 void factor_vm::primitive_strip_stack_traces() {
231 stack_trace_stripper stripper;
232 each_code_block(stripper);
235 struct code_block_accumulator {
236 std::vector<cell> objects;
238 void operator()(code_block* compiled, cell size) {
239 objects.push_back(compiled->owner);
240 objects.push_back(compiled->parameters);
241 objects.push_back(compiled->relocation);
243 objects.push_back(tag_fixnum(compiled->type()));
244 objects.push_back(tag_fixnum(compiled->size()));
246 /* Note: the entry point is always a multiple of the heap
247 alignment (16 bytes). We cannot allocate while iterating
248 through the code heap, so it is not possible to call
249 from_unsigned_cell() here. It is OK, however, to add it as
250 if it were a fixnum, and have library code shift it to the
252 cell entry_point = compiled->entry_point();
253 FACTOR_ASSERT((entry_point & (data_alignment - 1)) == 0);
254 FACTOR_ASSERT((entry_point & TAG_MASK) == FIXNUM_TYPE);
255 objects.push_back(entry_point);
259 /* Allocates memory */
260 cell factor_vm::code_blocks() {
261 code_block_accumulator accum;
262 each_code_block(accum);
263 return std_vector_to_array(accum.objects);
266 /* Allocates memory */
267 void factor_vm::primitive_code_blocks() { ctx->push(code_blocks()); }