]> gitweb.factorcode.org Git - factor.git/blob - vm/code_heap.cpp
vm: implement frame-based SEH for 64-bit Windows
[factor.git] / vm / code_heap.cpp
1 #include "master.hpp"
2
3 namespace factor
4 {
5
6 code_heap::code_heap(cell size)
7 {
8         if(size > ((u64)1 << (sizeof(cell) * 8 - 6))) fatal_error("Heap too large",size);
9         seg = new segment(align_page(size),true);
10         if(!seg) fatal_error("Out of memory in code_heap constructor",size);
11
12         cell start = seg->start + seh_area_size;
13
14         allocator = new free_list_allocator<code_block>(seg->end - start,start);
15
16         /* See os-windows-nt-x86.64.cpp for seh_area usage */
17         seh_area = (char *)seg->start;
18 }
19
20 code_heap::~code_heap()
21 {
22         delete allocator;
23         allocator = NULL;
24         delete seg;
25         seg = NULL;
26 }
27
28 void code_heap::write_barrier(code_block *compiled)
29 {
30         points_to_nursery.insert(compiled);
31         points_to_aging.insert(compiled);
32 }
33
34 void code_heap::clear_remembered_set()
35 {
36         points_to_nursery.clear();
37         points_to_aging.clear();
38 }
39
40 bool code_heap::uninitialized_p(code_block *compiled)
41 {
42         return uninitialized_blocks.count(compiled) > 0;
43 }
44
45 bool code_heap::marked_p(code_block *compiled)
46 {
47         return allocator->state.marked_p(compiled);
48 }
49
50 void code_heap::set_marked_p(code_block *compiled)
51 {
52         allocator->state.set_marked_p(compiled);
53 }
54
55 void code_heap::clear_mark_bits()
56 {
57         allocator->state.clear_mark_bits();
58 }
59
60 void code_heap::free(code_block *compiled)
61 {
62         assert(!uninitialized_p(compiled));
63         points_to_nursery.erase(compiled);
64         points_to_aging.erase(compiled);
65         allocator->free(compiled);
66 }
67
68 void code_heap::flush_icache()
69 {
70         factor::flush_icache(seg->start,seg->size);
71 }
72
73 /* Allocate a code heap during startup */
74 void factor_vm::init_code_heap(cell size)
75 {
76         code = new code_heap(size);
77 }
78
79 bool factor_vm::in_code_heap_p(cell ptr)
80 {
81         return (ptr >= code->seg->start && ptr <= code->seg->end);
82 }
83
84 struct word_updater {
85         factor_vm *parent;
86         bool reset_inline_caches;
87
88         word_updater(factor_vm *parent_, bool reset_inline_caches_) :
89                 parent(parent_), reset_inline_caches(reset_inline_caches_) {}
90
91         void operator()(code_block *compiled, cell size)
92         {
93                 parent->update_word_references(compiled,reset_inline_caches);
94         }
95 };
96
97 /* Update pointers to words referenced from all code blocks.
98 Only needed after redefining an existing word.
99 If generic words were redefined, inline caches need to be reset. */
100 void factor_vm::update_code_heap_words(bool reset_inline_caches)
101 {
102         word_updater updater(this,reset_inline_caches);
103         each_code_block(updater);
104 }
105
106 /* Fix up new words only.
107 Fast path for compilation units that only define new words. */
108 void factor_vm::initialize_code_blocks()
109 {
110         std::map<code_block *, cell>::const_iterator iter = code->uninitialized_blocks.begin();
111         std::map<code_block *, cell>::const_iterator end = code->uninitialized_blocks.end();
112
113         for(; iter != end; iter++)
114                 initialize_code_block(iter->first,iter->second);
115
116         code->uninitialized_blocks.clear();
117 }
118
119 void factor_vm::primitive_modify_code_heap()
120 {
121         bool reset_inline_caches = to_boolean(ctx->pop());
122         bool update_existing_words = to_boolean(ctx->pop());
123         data_root<array> alist(ctx->pop(),this);
124
125         cell count = array_capacity(alist.untagged());
126
127         if(count == 0)
128                 return;
129
130         for(cell i = 0; i < count; i++)
131         {
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                 {
139                 case QUOTATION_TYPE:
140                         jit_compile_word(word.value(),data.value(),false);
141                         break;
142                 case ARRAY_TYPE:
143                         {
144                                 array *compiled_data = data.as<array>().untagged();
145                                 cell parameters = array_nth(compiled_data,0);
146                                 cell literals = array_nth(compiled_data,1);
147                                 cell relocation = array_nth(compiled_data,2);
148                                 cell labels = array_nth(compiled_data,3);
149                                 cell code = array_nth(compiled_data,4);
150
151                                 code_block *compiled = add_code_block(
152                                         code_block_optimized,
153                                         code,
154                                         labels,
155                                         word.value(),
156                                         relocation,
157                                         parameters,
158                                         literals);
159
160                                 word->code = compiled;
161                         }
162                         break;
163                 default:
164                         critical_error("Expected a quotation or an array",data.value());
165                         break;
166                 }
167
168                 update_word_entry_point(word.untagged());
169         }
170
171         if(update_existing_words)
172                 update_code_heap_words(reset_inline_caches);
173         else
174                 initialize_code_blocks();
175 }
176
177 code_heap_room factor_vm::code_room()
178 {
179         code_heap_room room;
180
181         room.size             = code->allocator->size;
182         room.occupied_space   = code->allocator->occupied_space();
183         room.total_free       = code->allocator->free_space();
184         room.contiguous_free  = code->allocator->largest_free_block();
185         room.free_block_count = code->allocator->free_block_count();
186
187         return room;
188 }
189
190 void factor_vm::primitive_code_room()
191 {
192         code_heap_room room = code_room();
193         ctx->push(tag<byte_array>(byte_array_from_value(&room)));
194 }
195
196 struct stack_trace_stripper {
197         explicit stack_trace_stripper() {}
198
199         void operator()(code_block *compiled, cell size)
200         {
201                 compiled->owner = false_object;
202         }
203 };
204
205 void factor_vm::primitive_strip_stack_traces()
206 {
207         stack_trace_stripper stripper;
208         each_code_block(stripper);
209 }
210
211 struct code_block_accumulator {
212         std::vector<cell> objects;
213
214         void operator()(code_block *compiled, cell size)
215         {
216                 objects.push_back(compiled->owner);
217                 objects.push_back(compiled->parameters);
218                 objects.push_back(compiled->relocation);
219
220                 objects.push_back(tag_fixnum(compiled->type()));
221                 objects.push_back(tag_fixnum(compiled->size()));
222
223                 /* Note: the entry point is always a multiple of the heap
224                 alignment (16 bytes). We cannot allocate while iterating
225                 through the code heap, so it is not possible to call allot_cell()
226                 here. It is OK, however, to add it as if it were a fixnum, and
227                 have library code shift it to the left by 4. */
228                 cell entry_point = (cell)compiled->entry_point();
229                 assert((entry_point & (data_alignment - 1)) == 0);
230                 assert((entry_point & TAG_MASK) == FIXNUM_TYPE);
231                 objects.push_back(entry_point);
232         }
233 };
234
235 cell factor_vm::code_blocks()
236 {
237         code_block_accumulator accum;
238         each_code_block(accum);
239         return std_vector_to_array(accum.objects);
240 }
241
242 void factor_vm::primitive_code_blocks()
243 {
244         ctx->push(code_blocks());
245 }
246
247 }