2004-07-16 02:26:21 -04:00
|
|
|
#include "factor.h"
|
|
|
|
|
2004-07-23 01:21:47 -04:00
|
|
|
void reset_datastack(void)
|
|
|
|
{
|
2004-11-06 21:03:35 -05:00
|
|
|
ds = ds_bot - CELLS;
|
2004-07-23 01:21:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void reset_callstack(void)
|
|
|
|
{
|
2004-11-06 21:03:35 -05:00
|
|
|
cs = cs_bot - CELLS;
|
2004-07-23 01:21:47 -04:00
|
|
|
}
|
|
|
|
|
2004-11-08 22:36:51 -05:00
|
|
|
void fix_stacks(void)
|
|
|
|
{
|
|
|
|
if(STACK_UNDERFLOW(ds,ds_bot))
|
|
|
|
reset_datastack();
|
|
|
|
else if(STACK_OVERFLOW(ds,ds_bot))
|
|
|
|
reset_datastack();
|
|
|
|
else if(STACK_UNDERFLOW(cs,cs_bot))
|
|
|
|
reset_callstack();
|
|
|
|
else if(STACK_OVERFLOW(cs,cs_bot))
|
|
|
|
reset_callstack();
|
|
|
|
}
|
|
|
|
|
2004-08-12 02:13:43 -04:00
|
|
|
void init_stacks(void)
|
|
|
|
{
|
2004-08-20 18:48:08 -04:00
|
|
|
ds_bot = (CELL)alloc_guarded(STACK_SIZE);
|
2004-08-12 02:13:43 -04:00
|
|
|
reset_datastack();
|
2004-08-20 18:48:08 -04:00
|
|
|
cs_bot = (CELL)alloc_guarded(STACK_SIZE);
|
2004-08-12 02:13:43 -04:00
|
|
|
reset_callstack();
|
2004-08-20 18:48:08 -04:00
|
|
|
callframe = userenv[BOOT_ENV];
|
2004-08-12 02:13:43 -04:00
|
|
|
}
|
|
|
|
|
2004-07-16 02:26:21 -04:00
|
|
|
void primitive_drop(void)
|
|
|
|
{
|
2004-08-12 17:36:36 -04:00
|
|
|
dpop();
|
2004-07-16 02:26:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void primitive_dup(void)
|
|
|
|
{
|
2004-08-12 17:36:36 -04:00
|
|
|
dpush(dpeek());
|
2004-07-16 02:26:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void primitive_swap(void)
|
|
|
|
{
|
2004-08-12 17:36:36 -04:00
|
|
|
CELL top = dpeek();
|
2004-11-06 21:03:35 -05:00
|
|
|
CELL next = get(ds - CELLS);
|
|
|
|
put(ds,next);
|
|
|
|
put(ds - CELLS,top);
|
2004-07-16 02:26:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void primitive_over(void)
|
|
|
|
{
|
2004-11-06 21:03:35 -05:00
|
|
|
dpush(get(ds - CELLS));
|
2004-07-16 02:26:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void primitive_pick(void)
|
|
|
|
{
|
2004-11-06 21:03:35 -05:00
|
|
|
dpush(get(ds - CELLS * 2));
|
2004-07-16 02:26:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void primitive_to_r(void)
|
|
|
|
{
|
2004-08-12 17:36:36 -04:00
|
|
|
cpush(dpop());
|
2004-07-16 02:26:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void primitive_from_r(void)
|
|
|
|
{
|
2004-08-12 17:36:36 -04:00
|
|
|
dpush(cpop());
|
2004-07-16 02:26:21 -04:00
|
|
|
}
|
|
|
|
|
2004-08-12 02:13:43 -04:00
|
|
|
VECTOR* stack_to_vector(CELL bottom, CELL top)
|
2004-07-16 02:26:21 -04:00
|
|
|
{
|
2004-11-06 21:03:35 -05:00
|
|
|
CELL depth = (top - bottom + CELLS) / CELLS;
|
2004-07-16 02:26:21 -04:00
|
|
|
VECTOR* v = vector(depth);
|
2004-11-27 22:26:05 -05:00
|
|
|
ARRAY* a = untag_array(v->array);
|
2004-08-12 17:36:36 -04:00
|
|
|
memcpy(a + 1,(void*)bottom,depth * CELLS);
|
2004-07-16 02:26:21 -04:00
|
|
|
v->top = depth;
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
void primitive_datastack(void)
|
|
|
|
{
|
2004-10-12 23:49:43 -04:00
|
|
|
maybe_garbage_collection();
|
2004-08-20 18:48:08 -04:00
|
|
|
dpush(tag_object(stack_to_vector(ds_bot,ds)));
|
2004-07-16 02:26:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void primitive_callstack(void)
|
|
|
|
{
|
2004-10-12 23:49:43 -04:00
|
|
|
maybe_garbage_collection();
|
2004-08-20 18:48:08 -04:00
|
|
|
dpush(tag_object(stack_to_vector(cs_bot,cs)));
|
2004-07-16 02:26:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns top of stack */
|
|
|
|
CELL vector_to_stack(VECTOR* vector, CELL bottom)
|
|
|
|
{
|
2004-08-12 17:36:36 -04:00
|
|
|
CELL start = bottom;
|
2004-07-16 02:26:21 -04:00
|
|
|
CELL len = vector->top * CELLS;
|
2004-11-27 22:26:05 -05:00
|
|
|
memcpy((void*)start,untag_array(vector->array) + 1,len);
|
2004-11-06 21:03:35 -05:00
|
|
|
return start + len - CELLS;
|
2004-07-16 02:26:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void primitive_set_datastack(void)
|
|
|
|
{
|
2004-08-20 18:48:08 -04:00
|
|
|
ds = vector_to_stack(untag_vector(dpop()),ds_bot);
|
2004-07-16 02:26:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void primitive_set_callstack(void)
|
|
|
|
{
|
2004-08-20 18:48:08 -04:00
|
|
|
cs = vector_to_stack(untag_vector(dpop()),cs_bot);
|
2004-07-16 02:26:21 -04:00
|
|
|
}
|