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