6 void factor_vm::reset_datastack()
8 ds = ds_bot - sizeof(cell);
11 void factor_vm::reset_retainstack()
13 rs = rs_bot - sizeof(cell);
16 static const cell stack_reserved = (64 * sizeof(cell));
18 void factor_vm::fix_stacks()
20 if(ds + sizeof(cell) < ds_bot || ds + stack_reserved >= ds_top) reset_datastack();
21 if(rs + sizeof(cell) < rs_bot || rs + stack_reserved >= rs_top) reset_retainstack();
24 /* called before entry into foreign C code. Note that ds and rs might
25 be stored in registers, so callbacks must save and restore the correct values */
26 void factor_vm::save_stacks()
30 stack_chain->datastack = ds;
31 stack_chain->retainstack = rs;
35 context *factor_vm::alloc_context()
41 new_context = unused_contexts;
42 unused_contexts = unused_contexts->next;
46 new_context = new context;
47 new_context->datastack_region = new segment(ds_size);
48 new_context->retainstack_region = new segment(rs_size);
54 void factor_vm::dealloc_context(context *old_context)
56 old_context->next = unused_contexts;
57 unused_contexts = old_context;
60 /* called on entry into a compiled callback */
61 void factor_vm::nest_stacks()
63 context *new_context = alloc_context();
65 new_context->callstack_bottom = (stack_frame *)-1;
66 new_context->callstack_top = (stack_frame *)-1;
68 /* note that these register values are not necessarily valid stack
69 pointers. they are merely saved non-volatile registers, and are
70 restored in unnest_stacks(). consider this scenario:
71 - factor code calls C function
72 - C function saves ds/cs registers (since they're non-volatile)
73 - C function clobbers them
74 - C function calls Factor callback
75 - Factor callback returns
76 - C function restores registers
77 - C function returns to Factor code */
78 new_context->datastack_save = ds;
79 new_context->retainstack_save = rs;
81 /* save per-callback userenv */
82 new_context->current_callback_save = userenv[CURRENT_CALLBACK_ENV];
83 new_context->catchstack_save = userenv[CATCHSTACK_ENV];
85 new_context->next = stack_chain;
86 stack_chain = new_context;
92 void nest_stacks(factor_vm *myvm)
94 return myvm->nest_stacks();
97 /* called when leaving a compiled callback */
98 void factor_vm::unnest_stacks()
100 ds = stack_chain->datastack_save;
101 rs = stack_chain->retainstack_save;
103 /* restore per-callback userenv */
104 userenv[CURRENT_CALLBACK_ENV] = stack_chain->current_callback_save;
105 userenv[CATCHSTACK_ENV] = stack_chain->catchstack_save;
107 context *old_stacks = stack_chain;
108 stack_chain = old_stacks->next;
109 dealloc_context(old_stacks);
112 void unnest_stacks(factor_vm *myvm)
114 return myvm->unnest_stacks();
117 /* called on startup */
118 void factor_vm::init_stacks(cell ds_size_, cell rs_size_)
123 unused_contexts = NULL;
126 bool factor_vm::stack_to_array(cell bottom, cell top)
128 fixnum depth = (fixnum)(top - bottom + sizeof(cell));
134 array *a = allot_array_internal<array>(depth / sizeof(cell));
135 memcpy(a + 1,(void*)bottom,depth);
136 dpush(tag<array>(a));
141 void factor_vm::primitive_datastack()
143 if(!stack_to_array(ds_bot,ds))
144 general_error(ERROR_DS_UNDERFLOW,F,F,NULL);
147 void factor_vm::primitive_retainstack()
149 if(!stack_to_array(rs_bot,rs))
150 general_error(ERROR_RS_UNDERFLOW,F,F,NULL);
153 /* returns pointer to top of stack */
154 cell factor_vm::array_to_stack(array *array, cell bottom)
156 cell depth = array_capacity(array) * sizeof(cell);
157 memcpy((void*)bottom,array + 1,depth);
158 return bottom + depth - sizeof(cell);
161 void factor_vm::primitive_set_datastack()
163 ds = array_to_stack(untag_check<array>(dpop()),ds_bot);
166 void factor_vm::primitive_set_retainstack()
168 rs = array_to_stack(untag_check<array>(dpop()),rs_bot);
171 /* Used to implement call( */
172 void factor_vm::primitive_check_datastack()
174 fixnum out = to_fixnum(dpop());
175 fixnum in = to_fixnum(dpop());
176 fixnum height = out - in;
177 array *saved_datastack = untag_check<array>(dpop());
178 fixnum saved_height = array_capacity(saved_datastack);
179 fixnum current_height = (ds - ds_bot + sizeof(cell)) / sizeof(cell);
180 if(current_height - height != saved_height)
185 for(i = 0; i < saved_height - in; i++)
187 if(((cell *)ds_bot)[i] != array_nth(saved_datastack,i))