7 void factorvm::reset_datastack()
9 ds = ds_bot - sizeof(cell);
12 void factorvm::reset_retainstack()
14 rs = rs_bot - sizeof(cell);
17 static const cell stack_reserved = (64 * sizeof(cell));
19 void factorvm::fix_stacks()
21 if(ds + sizeof(cell) < ds_bot || ds + stack_reserved >= ds_top) reset_datastack();
22 if(rs + sizeof(cell) < rs_bot || rs + stack_reserved >= rs_top) reset_retainstack();
25 /* called before entry into foreign C code. Note that ds and rs might
26 be stored in registers, so callbacks must save and restore the correct values */
27 void factorvm::save_stacks()
31 stack_chain->datastack = ds;
32 stack_chain->retainstack = rs;
36 context *factorvm::alloc_context()
42 new_context = unused_contexts;
43 unused_contexts = unused_contexts->next;
47 new_context = (context *)safe_malloc(sizeof(context));
48 new_context->datastack_region = alloc_segment(ds_size);
49 new_context->retainstack_region = alloc_segment(rs_size);
55 void factorvm::dealloc_context(context *old_context)
57 old_context->next = unused_contexts;
58 unused_contexts = old_context;
61 /* called on entry into a compiled callback */
62 void factorvm::nest_stacks()
64 context *new_context = alloc_context();
66 new_context->callstack_bottom = (stack_frame *)-1;
67 new_context->callstack_top = (stack_frame *)-1;
69 /* note that these register values are not necessarily valid stack
70 pointers. they are merely saved non-volatile registers, and are
71 restored in unnest_stacks(). consider this scenario:
72 - factor code calls C function
73 - C function saves ds/cs registers (since they're non-volatile)
74 - C function clobbers them
75 - C function calls Factor callback
76 - Factor callback returns
77 - C function restores registers
78 - C function returns to Factor code */
79 new_context->datastack_save = ds;
80 new_context->retainstack_save = rs;
82 /* save per-callback userenv */
83 new_context->current_callback_save = userenv[CURRENT_CALLBACK_ENV];
84 new_context->catchstack_save = userenv[CATCHSTACK_ENV];
86 new_context->next = stack_chain;
87 stack_chain = new_context;
95 return vm->nest_stacks();
98 /* called when leaving a compiled callback */
99 void factorvm::unnest_stacks()
101 ds = stack_chain->datastack_save;
102 rs = stack_chain->retainstack_save;
104 /* restore per-callback userenv */
105 userenv[CURRENT_CALLBACK_ENV] = stack_chain->current_callback_save;
106 userenv[CATCHSTACK_ENV] = stack_chain->catchstack_save;
108 context *old_stacks = stack_chain;
109 stack_chain = old_stacks->next;
110 dealloc_context(old_stacks);
115 return vm->unnest_stacks();
118 /* called on startup */
119 void factorvm::init_stacks(cell ds_size_, cell rs_size_)
124 unused_contexts = NULL;
127 bool factorvm::stack_to_array(cell bottom, cell top)
129 fixnum depth = (fixnum)(top - bottom + sizeof(cell));
135 array *a = allot_array_internal<array>(depth / sizeof(cell));
136 memcpy(a + 1,(void*)bottom,depth);
137 dpush(tag<array>(a));
142 inline void factorvm::vmprim_datastack()
144 if(!stack_to_array(ds_bot,ds))
145 general_error(ERROR_DS_UNDERFLOW,F,F,NULL);
150 PRIMITIVE_GETVM()->vmprim_datastack();
153 inline void factorvm::vmprim_retainstack()
155 if(!stack_to_array(rs_bot,rs))
156 general_error(ERROR_RS_UNDERFLOW,F,F,NULL);
159 PRIMITIVE(retainstack)
161 PRIMITIVE_GETVM()->vmprim_retainstack();
164 /* returns pointer to top of stack */
165 cell factorvm::array_to_stack(array *array, cell bottom)
167 cell depth = array_capacity(array) * sizeof(cell);
168 memcpy((void*)bottom,array + 1,depth);
169 return bottom + depth - sizeof(cell);
172 inline void factorvm::vmprim_set_datastack()
174 ds = array_to_stack(untag_check<array>(dpop()),ds_bot);
177 PRIMITIVE(set_datastack)
179 PRIMITIVE_GETVM()->vmprim_set_datastack();
182 inline void factorvm::vmprim_set_retainstack()
184 rs = array_to_stack(untag_check<array>(dpop()),rs_bot);
187 PRIMITIVE(set_retainstack)
189 PRIMITIVE_GETVM()->vmprim_set_retainstack();
192 /* Used to implement call( */
193 inline void factorvm::vmprim_check_datastack()
195 fixnum out = to_fixnum(dpop());
196 fixnum in = to_fixnum(dpop());
197 fixnum height = out - in;
198 array *saved_datastack = untag_check<array>(dpop());
199 fixnum saved_height = array_capacity(saved_datastack);
200 fixnum current_height = (ds - ds_bot + sizeof(cell)) / sizeof(cell);
201 if(current_height - height != saved_height)
206 for(i = 0; i < saved_height - in; i++)
208 if(((cell *)ds_bot)[i] != array_nth(saved_datastack,i))
218 PRIMITIVE(check_datastack)
220 PRIMITIVE_GETVM()->vmprim_check_datastack();