6 context::context(cell datastack_size, cell retainstack_size, cell callstack_size) :
8 callstack_bottom(NULL),
12 datastack_seg(new segment(datastack_size,false)),
13 retainstack_seg(new segment(retainstack_size,false)),
14 callstack_seg(new segment(callstack_size,false))
19 void context::reset_datastack()
21 datastack = datastack_seg->start - sizeof(cell);
24 void context::reset_retainstack()
26 retainstack = retainstack_seg->start - sizeof(cell);
29 void context::reset_callstack()
31 callstack_top = callstack_bottom = CALLSTACK_BOTTOM(this);
34 void context::reset_context_objects()
36 memset_cell(context_objects,false_object,context_object_count * sizeof(cell));
44 reset_context_objects();
47 void context::fix_stacks()
49 if(datastack + sizeof(cell) < datastack_seg->start
50 || datastack + stack_reserved >= datastack_seg->end)
53 if(retainstack + sizeof(cell) < retainstack_seg->start
54 || retainstack + stack_reserved >= retainstack_seg->end)
58 void context::scrub_stacks(gc_info *info, cell index)
60 u8 *bitmap = info->gc_info_bitmap();
63 cell base = info->callsite_scrub_d(index);
65 for(cell loc = 0; loc < info->scrub_d_count; loc++)
67 if(bitmap_p(bitmap,base + loc))
70 std::cout << "scrubbing datastack location " << loc << std::endl;
72 *((cell *)datastack - loc) = 0;
78 cell base = info->callsite_scrub_r(index);
80 for(cell loc = 0; loc < info->scrub_r_count; loc++)
82 if(bitmap_p(bitmap,base + loc))
85 std::cout << "scrubbing retainstack location " << loc << std::endl;
87 *((cell *)retainstack - loc) = 0;
96 delete retainstack_seg;
100 /* called on startup */
101 void factor_vm::init_contexts(cell datastack_size_, cell retainstack_size_, cell callstack_size_)
103 datastack_size = datastack_size_;
104 retainstack_size = retainstack_size_;
105 callstack_size = callstack_size_;
108 spare_ctx = new_context();
111 void factor_vm::delete_contexts()
114 std::list<context *>::const_iterator iter = unused_contexts.begin();
115 std::list<context *>::const_iterator end = unused_contexts.end();
123 context *factor_vm::new_context()
125 context *new_context;
127 if(unused_contexts.empty())
129 new_context = new context(datastack_size,
135 new_context = unused_contexts.back();
136 unused_contexts.pop_back();
139 new_context->reset();
141 active_contexts.insert(new_context);
146 /* Allocates memory */
147 void factor_vm::init_context(context *ctx)
149 ctx->context_objects[OBJ_CONTEXT] = allot_alien(ctx);
152 /* Allocates memory */
153 context *new_context(factor_vm *parent)
155 context *new_context = parent->new_context();
156 parent->init_context(new_context);
160 void factor_vm::delete_context(context *old_context)
162 unused_contexts.push_back(old_context);
163 active_contexts.erase(old_context);
165 while(unused_contexts.size() > 10)
167 context *stale_context = unused_contexts.front();
168 unused_contexts.pop_front();
169 delete stale_context;
173 VM_C_API void delete_context(factor_vm *parent, context *old_context)
175 parent->delete_context(old_context);
178 /* Allocates memory */
179 VM_C_API void reset_context(factor_vm *parent, context *ctx)
182 parent->init_context(ctx);
185 /* Allocates memory */
186 cell factor_vm::begin_callback(cell quot_)
188 data_root<object> quot(quot_,this);
191 spare_ctx = new_context();
192 callback_ids.push_back(callback_id++);
199 cell begin_callback(factor_vm *parent, cell quot)
201 return parent->begin_callback(quot);
204 void factor_vm::end_callback()
206 callback_ids.pop_back();
210 void end_callback(factor_vm *parent)
212 parent->end_callback();
215 void factor_vm::primitive_current_callback()
217 ctx->push(tag_fixnum(callback_ids.back()));
220 void factor_vm::primitive_context_object()
222 fixnum n = untag_fixnum(ctx->peek());
223 ctx->replace(ctx->context_objects[n]);
226 void factor_vm::primitive_set_context_object()
228 fixnum n = untag_fixnum(ctx->pop());
229 cell value = ctx->pop();
230 ctx->context_objects[n] = value;
233 void factor_vm::primitive_context_object_for()
235 context *other_ctx = (context *)pinned_alien_offset(ctx->pop());
236 fixnum n = untag_fixnum(ctx->pop());
237 ctx->push(other_ctx->context_objects[n]);
240 /* Allocates memory */
241 cell factor_vm::stack_to_array(cell bottom, cell top)
243 fixnum depth = (fixnum)(top - bottom + sizeof(cell));
249 array *a = allot_uninitialized_array<array>(depth / sizeof(cell));
250 memcpy(a + 1,(void*)bottom,depth);
251 return tag<array>(a);
255 cell factor_vm::datastack_to_array(context *ctx)
257 cell array = stack_to_array(ctx->datastack_seg->start,ctx->datastack);
258 if(array == false_object)
260 general_error(ERROR_DATASTACK_UNDERFLOW,false_object,false_object);
267 void factor_vm::primitive_datastack()
269 ctx->push(datastack_to_array(ctx));
272 void factor_vm::primitive_datastack_for()
274 context *other_ctx = (context *)pinned_alien_offset(ctx->pop());
275 ctx->push(datastack_to_array(other_ctx));
278 cell factor_vm::retainstack_to_array(context *ctx)
280 cell array = stack_to_array(ctx->retainstack_seg->start,ctx->retainstack);
281 if(array == false_object)
283 general_error(ERROR_RETAINSTACK_UNDERFLOW,false_object,false_object);
290 void factor_vm::primitive_retainstack()
292 ctx->push(retainstack_to_array(ctx));
295 void factor_vm::primitive_retainstack_for()
297 context *other_ctx = (context *)pinned_alien_offset(ctx->pop());
298 ctx->push(retainstack_to_array(other_ctx));
301 /* returns pointer to top of stack */
302 cell factor_vm::array_to_stack(array *array, cell bottom)
304 cell depth = array_capacity(array) * sizeof(cell);
305 memcpy((void*)bottom,array + 1,depth);
306 return bottom + depth - sizeof(cell);
309 void factor_vm::set_datastack(context *ctx, array *array)
311 ctx->datastack = array_to_stack(array,ctx->datastack_seg->start);
314 void factor_vm::primitive_set_datastack()
316 set_datastack(ctx,untag_check<array>(ctx->pop()));
319 void factor_vm::set_retainstack(context *ctx, array *array)
321 ctx->retainstack = array_to_stack(array,ctx->retainstack_seg->start);
324 void factor_vm::primitive_set_retainstack()
326 set_retainstack(ctx,untag_check<array>(ctx->pop()));
329 /* Used to implement call( */
330 void factor_vm::primitive_check_datastack()
332 fixnum out = to_fixnum(ctx->pop());
333 fixnum in = to_fixnum(ctx->pop());
334 fixnum height = out - in;
335 array *saved_datastack = untag_check<array>(ctx->pop());
336 fixnum saved_height = array_capacity(saved_datastack);
337 fixnum current_height = (ctx->datastack - ctx->datastack_seg->start + sizeof(cell)) / sizeof(cell);
338 if(current_height - height != saved_height)
339 ctx->push(false_object);
342 cell *ds_bot = (cell *)ctx->datastack_seg->start;
343 for(fixnum i = 0; i < saved_height - in; i++)
345 if(ds_bot[i] != array_nth(saved_datastack,i))
347 ctx->push(false_object);
351 ctx->push(true_object);
355 void factor_vm::primitive_load_locals()
357 fixnum count = untag_fixnum(ctx->pop());
358 memcpy((cell *)(ctx->retainstack + sizeof(cell)),
359 (cell *)(ctx->datastack - sizeof(cell) * (count - 1)),
360 sizeof(cell) * count);
361 ctx->datastack -= sizeof(cell) * count;
362 ctx->retainstack += sizeof(cell) * count;