]> gitweb.factorcode.org Git - factor.git/blob - vm/contexts.cpp
VM: Refactor contexts to Factor style
[factor.git] / vm / contexts.cpp
1 #include "master.hpp"
2
3 namespace factor {
4
5 context::context(cell datastack_size, cell retainstack_size,
6                  cell callstack_size)
7     : callstack_top(NULL),
8       callstack_bottom(NULL),
9       datastack(0),
10       retainstack(0),
11       callstack_save(0),
12       datastack_seg(new segment(datastack_size, false)),
13       retainstack_seg(new segment(retainstack_size, false)),
14       callstack_seg(new segment(callstack_size, false)) {
15   reset();
16 }
17
18 void context::reset_datastack() {
19   datastack = datastack_seg->start - sizeof(cell);
20 }
21
22 void context::reset_retainstack() {
23   retainstack = retainstack_seg->start - sizeof(cell);
24 }
25
26 void context::reset_callstack() {
27   callstack_top = callstack_bottom = CALLSTACK_BOTTOM(this);
28 }
29
30 void context::reset_context_objects() {
31   memset_cell(context_objects, false_object,
32               context_object_count * sizeof(cell));
33 }
34
35 void context::reset() {
36   reset_datastack();
37   reset_retainstack();
38   reset_callstack();
39   reset_context_objects();
40 }
41
42 void context::fix_stacks() {
43   if (datastack + sizeof(cell) < datastack_seg->start ||
44       datastack + stack_reserved >= datastack_seg->end)
45     reset_datastack();
46
47   if (retainstack + sizeof(cell) < retainstack_seg->start ||
48       retainstack + stack_reserved >= retainstack_seg->end)
49     reset_retainstack();
50 }
51
52 void context::scrub_stacks(gc_info* info, cell index) {
53   u8* bitmap = info->gc_info_bitmap();
54
55   {
56     cell base = info->callsite_scrub_d(index);
57
58     for (cell loc = 0; loc < info->scrub_d_count; loc++) {
59       if (bitmap_p(bitmap, base + loc)) {
60 #ifdef DEBUG_GC_MAPS
61         std::cout << "scrubbing datastack location " << loc << std::endl;
62 #endif
63         *((cell*)datastack - loc) = 0;
64       }
65     }
66   }
67
68   {
69     cell base = info->callsite_scrub_r(index);
70
71     for (cell loc = 0; loc < info->scrub_r_count; loc++) {
72       if (bitmap_p(bitmap, base + loc)) {
73 #ifdef DEBUG_GC_MAPS
74         std::cout << "scrubbing retainstack location " << loc << std::endl;
75 #endif
76         *((cell*)retainstack - loc) = 0;
77       }
78     }
79   }
80 }
81
82 context::~context() {
83   delete datastack_seg;
84   delete retainstack_seg;
85   delete callstack_seg;
86 }
87
88 /* called on startup */
89 void factor_vm::init_contexts(cell datastack_size_, cell retainstack_size_,
90                               cell callstack_size_) {
91   datastack_size = datastack_size_;
92   retainstack_size = retainstack_size_;
93   callstack_size = callstack_size_;
94
95   ctx = NULL;
96   spare_ctx = new_context();
97 }
98
99 void factor_vm::delete_contexts() {
100   FACTOR_ASSERT(!ctx);
101   std::list<context*>::const_iterator iter = unused_contexts.begin();
102   std::list<context*>::const_iterator end = unused_contexts.end();
103   while (iter != end) {
104     delete *iter;
105     iter++;
106   }
107 }
108
109 context* factor_vm::new_context() {
110   context* new_context;
111
112   if (unused_contexts.empty()) {
113     new_context = new context(datastack_size, retainstack_size, callstack_size);
114   } else {
115     new_context = unused_contexts.back();
116     unused_contexts.pop_back();
117   }
118
119   new_context->reset();
120
121   active_contexts.insert(new_context);
122
123   return new_context;
124 }
125
126 /* Allocates memory */
127 void factor_vm::init_context(context* ctx) {
128   ctx->context_objects[OBJ_CONTEXT] = allot_alien(ctx);
129 }
130
131 /* Allocates memory */
132 context* new_context(factor_vm* parent) {
133   context* new_context = parent->new_context();
134   parent->init_context(new_context);
135   return new_context;
136 }
137
138 void factor_vm::delete_context(context* old_context) {
139   unused_contexts.push_back(old_context);
140   active_contexts.erase(old_context);
141
142   while (unused_contexts.size() > 10) {
143     context* stale_context = unused_contexts.front();
144     unused_contexts.pop_front();
145     delete stale_context;
146   }
147 }
148
149 VM_C_API void delete_context(factor_vm* parent, context* old_context) {
150   parent->delete_context(old_context);
151 }
152
153 /* Allocates memory */
154 VM_C_API void reset_context(factor_vm* parent, context* ctx) {
155   ctx->reset();
156   parent->init_context(ctx);
157 }
158
159 /* Allocates memory */
160 cell factor_vm::begin_callback(cell quot_) {
161   data_root<object> quot(quot_, this);
162
163   ctx->reset();
164   spare_ctx = new_context();
165   callback_ids.push_back(callback_id++);
166
167   init_context(ctx);
168
169   return quot.value();
170 }
171
172 cell begin_callback(factor_vm* parent, cell quot) {
173   return parent->begin_callback(quot);
174 }
175
176 void factor_vm::end_callback() {
177   callback_ids.pop_back();
178   delete_context(ctx);
179 }
180
181 void end_callback(factor_vm* parent) { parent->end_callback(); }
182
183 void factor_vm::primitive_current_callback() {
184   ctx->push(tag_fixnum(callback_ids.back()));
185 }
186
187 void factor_vm::primitive_context_object() {
188   fixnum n = untag_fixnum(ctx->peek());
189   ctx->replace(ctx->context_objects[n]);
190 }
191
192 void factor_vm::primitive_set_context_object() {
193   fixnum n = untag_fixnum(ctx->pop());
194   cell value = ctx->pop();
195   ctx->context_objects[n] = value;
196 }
197
198 void factor_vm::primitive_context_object_for() {
199   context* other_ctx = (context*)pinned_alien_offset(ctx->pop());
200   fixnum n = untag_fixnum(ctx->peek());
201   ctx->replace(other_ctx->context_objects[n]);
202 }
203
204 /* Allocates memory */
205 cell factor_vm::stack_to_array(cell bottom, cell top) {
206   fixnum depth = (fixnum)(top - bottom + sizeof(cell));
207
208   if (depth < 0)
209     return false_object;
210   else {
211     array* a = allot_uninitialized_array<array>(depth / sizeof(cell));
212     memcpy(a + 1, (void*)bottom, depth);
213     return tag<array>(a);
214   }
215 }
216
217 cell factor_vm::datastack_to_array(context* ctx) {
218   cell array = stack_to_array(ctx->datastack_seg->start, ctx->datastack);
219   if (array == false_object) {
220     general_error(ERROR_DATASTACK_UNDERFLOW, false_object, false_object);
221     return false_object;
222   } else
223     return array;
224 }
225
226 void factor_vm::primitive_datastack() { ctx->push(datastack_to_array(ctx)); }
227
228 void factor_vm::primitive_datastack_for() {
229   context* other_ctx = (context*)pinned_alien_offset(ctx->peek());
230   ctx->replace(datastack_to_array(other_ctx));
231 }
232
233 cell factor_vm::retainstack_to_array(context* ctx) {
234   cell array = stack_to_array(ctx->retainstack_seg->start, ctx->retainstack);
235   if (array == false_object) {
236     general_error(ERROR_RETAINSTACK_UNDERFLOW, false_object, false_object);
237     return false_object;
238   } else
239     return array;
240 }
241
242 void factor_vm::primitive_retainstack() {
243   ctx->push(retainstack_to_array(ctx));
244 }
245
246 void factor_vm::primitive_retainstack_for() {
247   context* other_ctx = (context*)pinned_alien_offset(ctx->peek());
248   ctx->replace(retainstack_to_array(other_ctx));
249 }
250
251 /* returns pointer to top of stack */
252 cell factor_vm::array_to_stack(array* array, cell bottom) {
253   cell depth = array_capacity(array) * sizeof(cell);
254   memcpy((void*)bottom, array + 1, depth);
255   return bottom + depth - sizeof(cell);
256 }
257
258 void factor_vm::set_datastack(context* ctx, array* array) {
259   ctx->datastack = array_to_stack(array, ctx->datastack_seg->start);
260 }
261
262 void factor_vm::primitive_set_datastack() {
263   set_datastack(ctx, untag_check<array>(ctx->pop()));
264 }
265
266 void factor_vm::set_retainstack(context* ctx, array* array) {
267   ctx->retainstack = array_to_stack(array, ctx->retainstack_seg->start);
268 }
269
270 void factor_vm::primitive_set_retainstack() {
271   set_retainstack(ctx, untag_check<array>(ctx->pop()));
272 }
273
274 /* Used to implement call( */
275 void factor_vm::primitive_check_datastack() {
276   fixnum out = to_fixnum(ctx->pop());
277   fixnum in = to_fixnum(ctx->pop());
278   fixnum height = out - in;
279   array* saved_datastack = untag_check<array>(ctx->pop());
280   fixnum saved_height = array_capacity(saved_datastack);
281   fixnum current_height =
282       (ctx->datastack - ctx->datastack_seg->start + sizeof(cell)) /
283       sizeof(cell);
284   if (current_height - height != saved_height)
285     ctx->push(false_object);
286   else {
287     cell* ds_bot = (cell*)ctx->datastack_seg->start;
288     for (fixnum i = 0; i < saved_height - in; i++) {
289       if (ds_bot[i] != array_nth(saved_datastack, i)) {
290         ctx->push(false_object);
291         return;
292       }
293     }
294     ctx->push(true_object);
295   }
296 }
297
298 void factor_vm::primitive_load_locals() {
299   fixnum count = untag_fixnum(ctx->pop());
300   memcpy((cell*)(ctx->retainstack + sizeof(cell)),
301          (cell*)(ctx->datastack - sizeof(cell) * (count - 1)),
302          sizeof(cell) * count);
303   ctx->datastack -= sizeof(cell) * count;
304   ctx->retainstack += sizeof(cell) * count;
305 }
306
307 }