]> gitweb.factorcode.org Git - factor.git/blob - vm/code_heap.cpp
vm: non-optimizing compiler now compiles word definition quotations with the owner...
[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 > (1L << (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::needs_fixup_p(code_block *compiled)
35 {
36         return needs_fixup.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::code_heap_free(code_block *compiled)
55 {
56         points_to_nursery.erase(compiled);
57         points_to_aging.erase(compiled);
58         needs_fixup.erase(compiled);
59         allocator->free(compiled);
60 }
61
62 /* Allocate a code heap during startup */
63 void factor_vm::init_code_heap(cell size)
64 {
65         code = new code_heap(size);
66 }
67
68 bool factor_vm::in_code_heap_p(cell ptr)
69 {
70         return (ptr >= code->seg->start && ptr <= code->seg->end);
71 }
72
73 struct word_updater {
74         factor_vm *parent;
75
76         explicit word_updater(factor_vm *parent_) : parent(parent_) {}
77
78         void operator()(code_block *compiled, cell size)
79         {
80                 parent->update_word_references(compiled);
81         }
82 };
83
84 /* Update pointers to words referenced from all code blocks. Only after
85 defining a new word. */
86 void factor_vm::update_code_heap_words()
87 {
88         word_updater updater(this);
89         iterate_code_heap(updater);
90 }
91
92 void factor_vm::primitive_modify_code_heap()
93 {
94         data_root<array> alist(dpop(),this);
95
96         cell count = array_capacity(alist.untagged());
97
98         if(count == 0)
99                 return;
100
101         for(cell i = 0; i < count; i++)
102         {
103                 data_root<array> pair(array_nth(alist.untagged(),i),this);
104
105                 data_root<word> word(array_nth(pair.untagged(),0),this);
106                 data_root<object> data(array_nth(pair.untagged(),1),this);
107
108                 switch(data.type())
109                 {
110                 case QUOTATION_TYPE:
111                         jit_compile_word(word.value(),data.value(),false);
112                         break;
113                 case ARRAY_TYPE:
114                         {
115                                 array *compiled_data = data.as<array>().untagged();
116                                 cell owner = array_nth(compiled_data,0);
117                                 cell literals = array_nth(compiled_data,1);
118                                 cell relocation = array_nth(compiled_data,2);
119                                 cell labels = array_nth(compiled_data,3);
120                                 cell code = array_nth(compiled_data,4);
121
122                                 code_block *compiled = add_code_block(
123                                         code_block_optimized,
124                                         code,
125                                         labels,
126                                         owner,
127                                         relocation,
128                                         literals);
129
130                                 word->code = compiled;
131                         }
132                         break;
133                 default:
134                         critical_error("Expected a quotation or an array",data.value());
135                         break;
136                 }
137
138                 update_word_xt(word.untagged());
139         }
140
141         update_code_heap_words();
142 }
143
144 code_heap_room factor_vm::code_room()
145 {
146         code_heap_room room;
147
148         room.size             = code->allocator->size;
149         room.occupied_space   = code->allocator->occupied_space();
150         room.total_free       = code->allocator->free_space();
151         room.contiguous_free  = code->allocator->largest_free_block();
152         room.free_block_count = code->allocator->free_block_count();
153
154         return room;
155 }
156
157 void factor_vm::primitive_code_room()
158 {
159         code_heap_room room = code_room();
160         dpush(tag<byte_array>(byte_array_from_value(&room)));
161 }
162
163 struct stack_trace_stripper {
164         explicit stack_trace_stripper() {}
165
166         void operator()(code_block *compiled, cell size)
167         {
168                 compiled->owner = false_object;
169         }
170 };
171
172 void factor_vm::primitive_strip_stack_traces()
173 {
174         stack_trace_stripper stripper;
175         iterate_code_heap(stripper);
176 }
177
178 }