]> gitweb.factorcode.org Git - factor.git/blob - vm/code_heap.cpp
vm: eliminating register variables work in progress. Works on x86-32 with non-optimiz...
[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 heap allocator",size);
11         allocator = new free_list_allocator<code_block>(size,seg->start);
12 }
13
14 code_heap::~code_heap()
15 {
16         delete allocator;
17         allocator = NULL;
18         delete seg;
19         seg = NULL;
20 }
21
22 void code_heap::write_barrier(code_block *compiled)
23 {
24         points_to_nursery.insert(compiled);
25         points_to_aging.insert(compiled);
26 }
27
28 void code_heap::clear_remembered_set()
29 {
30         points_to_nursery.clear();
31         points_to_aging.clear();
32 }
33
34 bool code_heap::uninitialized_p(code_block *compiled)
35 {
36         return uninitialized_blocks.count(compiled) > 0;
37 }
38
39 bool code_heap::marked_p(code_block *compiled)
40 {
41         return allocator->state.marked_p(compiled);
42 }
43
44 void code_heap::set_marked_p(code_block *compiled)
45 {
46         allocator->state.set_marked_p(compiled);
47 }
48
49 void code_heap::clear_mark_bits()
50 {
51         allocator->state.clear_mark_bits();
52 }
53
54 void code_heap::free(code_block *compiled)
55 {
56         assert(!uninitialized_p(compiled));
57         points_to_nursery.erase(compiled);
58         points_to_aging.erase(compiled);
59         allocator->free(compiled);
60 }
61
62 void code_heap::flush_icache()
63 {
64         factor::flush_icache(seg->start,seg->size);
65 }
66
67 /* Allocate a code heap during startup */
68 void factor_vm::init_code_heap(cell size)
69 {
70         code = new code_heap(size);
71 }
72
73 bool factor_vm::in_code_heap_p(cell ptr)
74 {
75         return (ptr >= code->seg->start && ptr <= code->seg->end);
76 }
77
78 struct word_updater {
79         factor_vm *parent;
80
81         explicit word_updater(factor_vm *parent_) : parent(parent_) {}
82
83         void operator()(code_block *compiled, cell size)
84         {
85                 parent->update_word_references(compiled);
86         }
87 };
88
89 /* Update pointers to words referenced from all code blocks. Only after
90 defining a new word. */
91 void factor_vm::update_code_heap_words()
92 {
93         word_updater updater(this);
94         each_code_block(updater);
95 }
96
97 void factor_vm::primitive_modify_code_heap()
98 {
99         data_root<array> alist(ctx->pop(),this);
100
101         cell count = array_capacity(alist.untagged());
102
103         if(count == 0)
104                 return;
105
106         for(cell i = 0; i < count; i++)
107         {
108                 data_root<array> pair(array_nth(alist.untagged(),i),this);
109
110                 data_root<word> word(array_nth(pair.untagged(),0),this);
111                 data_root<object> data(array_nth(pair.untagged(),1),this);
112
113                 switch(data.type())
114                 {
115                 case QUOTATION_TYPE:
116                         jit_compile_word(word.value(),data.value(),false);
117                         break;
118                 case ARRAY_TYPE:
119                         {
120                                 array *compiled_data = data.as<array>().untagged();
121                                 cell parameters = array_nth(compiled_data,0);
122                                 cell literals = array_nth(compiled_data,1);
123                                 cell relocation = array_nth(compiled_data,2);
124                                 cell labels = array_nth(compiled_data,3);
125                                 cell code = array_nth(compiled_data,4);
126
127                                 code_block *compiled = add_code_block(
128                                         code_block_optimized,
129                                         code,
130                                         labels,
131                                         word.value(),
132                                         relocation,
133                                         parameters,
134                                         literals);
135
136                                 word->code = compiled;
137                         }
138                         break;
139                 default:
140                         critical_error("Expected a quotation or an array",data.value());
141                         break;
142                 }
143
144                 update_word_xt(word.untagged());
145         }
146
147         update_code_heap_words();
148 }
149
150 code_heap_room factor_vm::code_room()
151 {
152         code_heap_room room;
153
154         room.size             = code->allocator->size;
155         room.occupied_space   = code->allocator->occupied_space();
156         room.total_free       = code->allocator->free_space();
157         room.contiguous_free  = code->allocator->largest_free_block();
158         room.free_block_count = code->allocator->free_block_count();
159
160         return room;
161 }
162
163 void factor_vm::primitive_code_room()
164 {
165         code_heap_room room = code_room();
166         ctx->push(tag<byte_array>(byte_array_from_value(&room)));
167 }
168
169 struct stack_trace_stripper {
170         explicit stack_trace_stripper() {}
171
172         void operator()(code_block *compiled, cell size)
173         {
174                 compiled->owner = false_object;
175         }
176 };
177
178 void factor_vm::primitive_strip_stack_traces()
179 {
180         stack_trace_stripper stripper;
181         each_code_block(stripper);
182 }
183
184 }