]> gitweb.factorcode.org Git - factor.git/blob - vm/code_heap.cpp
vm: bring back FACTOR_ASSERT.
[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 - 6)))
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 = (void*)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::sweep() {
53   auto clear_free_blocks_from_all_blocks = [&](code_block* block, cell size) {
54     std::set<cell>::iterator erase_from =
55       all_blocks.lower_bound((cell)block);
56     std::set<cell>::iterator erase_to =
57       all_blocks.lower_bound((cell)block + size);
58     all_blocks.erase(erase_from, erase_to);
59   };
60   allocator->sweep(clear_free_blocks_from_all_blocks);
61 #ifdef FACTOR_DEBUG
62   verify_all_blocks_set();
63 #endif
64 }
65
66 void code_heap::verify_all_blocks_set() {
67   auto all_blocks_set_verifier = [&](code_block* block, cell size) {
68     FACTOR_ASSERT(all_blocks.find((cell)block) != all_blocks.end());
69   };
70   allocator->iterate(all_blocks_set_verifier);
71 }
72
73 code_block* code_heap::code_block_for_address(cell address) {
74   std::set<cell>::const_iterator blocki = all_blocks.upper_bound(address);
75   FACTOR_ASSERT(blocki != all_blocks.begin());
76   --blocki;
77   code_block* found_block = (code_block*)*blocki;
78   FACTOR_ASSERT(found_block->entry_point() <=
79                 address /* XXX this isn't valid during fixup. should store the
80                                size in the map
81                               && address - found_block->entry_point() <
82                                  found_block->size()*/);
83   return found_block;
84 }
85
86 void code_heap::initialize_all_blocks_set() {
87   all_blocks.clear();
88   auto all_blocks_set_inserter = [&](code_block* block, cell size) {
89     all_blocks.insert((cell)block);
90   };
91   allocator->iterate(all_blocks_set_inserter);
92 #ifdef FACTOR_DEBUG
93   verify_all_blocks_set();
94 #endif
95 }
96
97 /* Allocate a code heap during startup */
98 void factor_vm::init_code_heap(cell size) { code = new code_heap(size); }
99
100 /* Update pointers to words referenced from all code blocks.
101 Only needed after redefining an existing word.
102 If generic words were redefined, inline caches need to be reset. */
103 void factor_vm::update_code_heap_words(bool reset_inline_caches) {
104   auto word_updater = [&](code_block* block, cell size) {
105     update_word_references(block, reset_inline_caches);
106   };
107   each_code_block(word_updater);
108 }
109
110 /* Fix up new words only.
111 Fast path for compilation units that only define new words. */
112 void factor_vm::initialize_code_blocks() {
113
114   FACTOR_FOR_EACH(code->uninitialized_blocks) {
115     initialize_code_block(iter->first, iter->second);
116   }
117   code->uninitialized_blocks.clear();
118 }
119
120 /* Allocates memory */
121 void factor_vm::primitive_modify_code_heap() {
122   bool reset_inline_caches = to_boolean(ctx->pop());
123   bool update_existing_words = to_boolean(ctx->pop());
124   data_root<array> alist(ctx->pop(), this);
125
126   cell count = array_capacity(alist.untagged());
127
128   if (count == 0)
129     return;
130
131   for (cell i = 0; i < count; i++) {
132     data_root<array> pair(array_nth(alist.untagged(), i), this);
133
134     data_root<word> word(array_nth(pair.untagged(), 0), this);
135     data_root<object> data(array_nth(pair.untagged(), 1), this);
136
137     switch (data.type()) {
138       case QUOTATION_TYPE:
139         jit_compile_word(word.value(), data.value(), false);
140         break;
141       case ARRAY_TYPE: {
142         array* compiled_data = data.as<array>().untagged();
143         cell parameters = array_nth(compiled_data, 0);
144         cell literals = array_nth(compiled_data, 1);
145         cell relocation = array_nth(compiled_data, 2);
146         cell labels = array_nth(compiled_data, 3);
147         cell code = array_nth(compiled_data, 4);
148         cell frame_size = untag_fixnum(array_nth(compiled_data, 5));
149
150         code_block* compiled =
151             add_code_block(code_block_optimized, code, labels, word.value(),
152                            relocation, parameters, literals, frame_size);
153
154         word->entry_point = compiled->entry_point();
155       } break;
156       default:
157         critical_error("Expected a quotation or an array", data.value());
158         break;
159     }
160   }
161
162   if (update_existing_words)
163     update_code_heap_words(reset_inline_caches);
164   else
165     initialize_code_blocks();
166 }
167
168 /* Allocates memory */
169 void factor_vm::primitive_code_room() {
170   allocator_room room = code->allocator->as_allocator_room();
171   ctx->push(tag<byte_array>(byte_array_from_value(&room)));
172 }
173
174 void factor_vm::primitive_strip_stack_traces() {
175   auto stack_trace_stripper = [](code_block* block, cell size) {
176     block->owner = false_object;
177   };
178   each_code_block(stack_trace_stripper);
179 }
180
181 /* Allocates memory */
182 cell factor_vm::code_blocks() {
183   std::vector<cell> objects;
184
185   auto code_block_accumulator = [&](code_block* block, cell size) {
186     objects.push_back(block->owner);
187     objects.push_back(block->parameters);
188     objects.push_back(block->relocation);
189
190     objects.push_back(tag_fixnum(block->type()));
191     objects.push_back(tag_fixnum(block->size()));
192
193     /* Note: the entry point is always a multiple of the heap
194        alignment (16 bytes). We cannot allocate while iterating
195        through the code heap, so it is not possible to call
196        from_unsigned_cell() here. It is OK, however, to add it as
197        if it were a fixnum, and have library code shift it to the
198        left by 4. */
199     cell entry_point = block->entry_point();
200     FACTOR_ASSERT((entry_point & (data_alignment - 1)) == 0);
201     FACTOR_ASSERT((entry_point & TAG_MASK) == FIXNUM_TYPE);
202     objects.push_back(entry_point);
203   };
204   each_code_block(code_block_accumulator);
205   return std_vector_to_array(objects);
206 }
207
208 /* Allocates memory */
209 void factor_vm::primitive_code_blocks() { ctx->push(code_blocks()); }
210
211 }