factor/native/stack.c

126 lines
2.0 KiB
C
Raw Normal View History

2004-07-16 02:26:21 -04:00
#include "factor.h"
void reset_datastack(void)
{
2004-08-20 18:48:08 -04:00
ds = ds_bot;
}
void reset_callstack(void)
{
2004-08-20 18:48:08 -04:00
cs = cs_bot;
}
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)
{
dpop();
2004-07-16 02:26:21 -04:00
}
void primitive_dup(void)
{
dpush(dpeek());
2004-07-16 02:26:21 -04:00
}
void primitive_swap(void)
{
CELL top = dpeek();
2004-08-20 18:48:08 -04:00
CELL next = get(ds - CELLS * 2);
put(ds - CELLS,next);
put(ds - CELLS * 2,top);
2004-07-16 02:26:21 -04:00
}
void primitive_over(void)
{
2004-08-20 18:48:08 -04:00
dpush(get(ds - CELLS * 2));
2004-07-16 02:26:21 -04:00
}
void primitive_pick(void)
{
2004-08-20 18:48:08 -04:00
dpush(get(ds - CELLS * 3));
2004-07-16 02:26:21 -04:00
}
void primitive_nip(void)
{
CELL top = dpop();
2004-08-20 18:48:08 -04:00
put(ds - CELLS,top);
2004-07-16 02:26:21 -04:00
}
void primitive_tuck(void)
{
CELL top = dpeek();
2004-08-20 18:48:08 -04:00
CELL next = get(ds - CELLS * 2);
put(ds - CELLS * 2,top);
put(ds - CELLS,next);
dpush(top);
2004-07-16 02:26:21 -04:00
}
void primitive_rot(void)
{
CELL top = dpeek();
2004-08-20 18:48:08 -04:00
CELL next = get(ds - CELLS * 2);
CELL next_next = get(ds - CELLS * 3);
put(ds - CELLS * 3,next);
put(ds - CELLS * 2,top);
put(ds - CELLS,next_next);
2004-07-16 02:26:21 -04:00
}
void primitive_to_r(void)
{
cpush(dpop());
2004-07-16 02:26:21 -04:00
}
void primitive_from_r(void)
{
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
{
CELL depth = (top - bottom) / CELLS;
2004-07-16 02:26:21 -04:00
VECTOR* v = vector(depth);
ARRAY* a = v->array;
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-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-09-22 23:42:45 -04:00
/* we don't want gc word to end up on callstack. */
gc_protect = true;
2004-08-20 18:48:08 -04:00
dpush(tag_object(stack_to_vector(cs_bot,cs)));
2004-09-22 23:42:45 -04:00
gc_protect = false;
2004-07-16 02:26:21 -04:00
}
/* Returns top of stack */
CELL vector_to_stack(VECTOR* vector, CELL bottom)
{
CELL start = bottom;
2004-07-16 02:26:21 -04:00
CELL len = vector->top * CELLS;
2004-08-04 22:43:58 -04:00
memcpy((void*)start,vector->array + 1,len);
2004-07-16 02:26:21 -04:00
return start + len;
}
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
}