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)
|
|
|
|
{
|
2006-02-23 02:09:34 -05:00
|
|
|
if(STACK_UNDERFLOW(ds,stack_chain->data_region))
|
2004-11-08 22:36:51 -05:00
|
|
|
reset_datastack();
|
2006-02-23 02:09:34 -05:00
|
|
|
else if(STACK_OVERFLOW(ds,stack_chain->data_region))
|
2004-11-08 22:36:51 -05:00
|
|
|
reset_datastack();
|
2006-02-23 02:09:34 -05:00
|
|
|
else if(STACK_UNDERFLOW(cs,stack_chain->call_region))
|
2004-11-08 22:36:51 -05:00
|
|
|
reset_callstack();
|
2006-02-23 02:09:34 -05:00
|
|
|
else if(STACK_OVERFLOW(cs,stack_chain->call_region))
|
2004-11-08 22:36:51 -05:00
|
|
|
reset_callstack();
|
|
|
|
}
|
|
|
|
|
2006-02-13 17:16:34 -05:00
|
|
|
/* called before entry into foreign C code. Note that ds and cs are stored
|
|
|
|
in registers, so callbacks must save and restore the correct values */
|
2006-02-13 02:46:07 -05:00
|
|
|
void save_stacks(void)
|
|
|
|
{
|
2006-02-23 02:09:34 -05:00
|
|
|
stack_chain->data = ds;
|
|
|
|
stack_chain->call = cs;
|
2006-02-13 02:46:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* called on entry into a compiled callback */
|
|
|
|
void nest_stacks(void)
|
|
|
|
{
|
2006-02-19 16:27:53 -05:00
|
|
|
STACKS *new_stacks = safe_malloc(sizeof(STACKS));
|
2006-02-13 17:16:34 -05:00
|
|
|
|
2006-02-14 23:23:08 -05:00
|
|
|
/* note that these register values are not necessarily valid stack
|
|
|
|
pointers. they are merely saved non-volatile registers, and are
|
|
|
|
restored in unnest_stacks(). consider this scenario:
|
|
|
|
- factor code calls C function
|
|
|
|
- C function saves ds/cs registers (since they're non-volatile)
|
|
|
|
- C function clobbers them
|
|
|
|
- C function calls Factor callback
|
|
|
|
- Factor callback returns
|
|
|
|
- C function restores registers
|
|
|
|
- C function returns to Factor code */
|
2006-02-23 02:09:34 -05:00
|
|
|
new_stacks->data_save = ds;
|
|
|
|
new_stacks->call_save = cs;
|
2006-02-23 19:01:12 -05:00
|
|
|
new_stacks->cards_offset = cards_offset;
|
2006-02-14 23:23:08 -05:00
|
|
|
|
2006-02-13 17:57:20 -05:00
|
|
|
new_stacks->callframe = callframe;
|
2006-02-23 02:09:34 -05:00
|
|
|
new_stacks->catch_save = userenv[CATCHSTACK_ENV];
|
|
|
|
|
|
|
|
new_stacks->data_region = alloc_bounded_block(ds_size);
|
|
|
|
new_stacks->call_region = alloc_bounded_block(cs_size);
|
2006-02-13 02:46:07 -05:00
|
|
|
new_stacks->next = stack_chain;
|
|
|
|
stack_chain = new_stacks;
|
2006-03-14 01:22:49 -05:00
|
|
|
|
2006-02-13 17:57:20 -05:00
|
|
|
callframe = F;
|
2006-02-13 02:46:07 -05:00
|
|
|
reset_datastack();
|
|
|
|
reset_callstack();
|
2006-03-14 01:22:49 -05:00
|
|
|
update_cards_offset();
|
2006-02-13 02:46:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* called when leaving a compiled callback */
|
|
|
|
void unnest_stacks(void)
|
|
|
|
{
|
2006-03-11 03:17:24 -05:00
|
|
|
STACKS *old_stacks = stack_chain;
|
|
|
|
|
2006-02-23 02:09:34 -05:00
|
|
|
dealloc_bounded_block(stack_chain->data_region);
|
|
|
|
dealloc_bounded_block(stack_chain->call_region);
|
2006-02-14 23:23:08 -05:00
|
|
|
|
2006-03-11 03:17:24 -05:00
|
|
|
ds = old_stacks->data_save;
|
|
|
|
cs = old_stacks->call_save;
|
|
|
|
cards_offset = old_stacks->cards_offset;
|
2006-02-14 23:23:08 -05:00
|
|
|
|
2006-03-11 03:17:24 -05:00
|
|
|
callframe = old_stacks->callframe;
|
|
|
|
userenv[CATCHSTACK_ENV] = old_stacks->catch_save;
|
2006-02-23 02:09:34 -05:00
|
|
|
|
2006-03-11 03:17:24 -05:00
|
|
|
stack_chain = old_stacks->next;
|
|
|
|
|
|
|
|
free(old_stacks);
|
2006-02-13 02:46:07 -05:00
|
|
|
}
|
|
|
|
|
2006-02-14 23:23:08 -05:00
|
|
|
/* called on startup */
|
2005-03-27 01:52:13 -05:00
|
|
|
void init_stacks(CELL ds_size_, CELL cs_size_)
|
2004-08-12 02:13:43 -04:00
|
|
|
{
|
2005-03-27 01:52:13 -05:00
|
|
|
ds_size = ds_size_;
|
|
|
|
cs_size = cs_size_;
|
2006-02-13 02:46:07 -05:00
|
|
|
stack_chain = NULL;
|
|
|
|
nest_stacks();
|
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
|
|
|
}
|
|
|
|
|
2005-09-05 03:06:47 -04:00
|
|
|
void primitive_2drop(void)
|
|
|
|
{
|
|
|
|
ds -= 2 * CELLS;
|
|
|
|
}
|
|
|
|
|
|
|
|
void primitive_3drop(void)
|
|
|
|
{
|
|
|
|
ds -= 3 * CELLS;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2005-09-05 03:06:47 -04:00
|
|
|
void primitive_2dup(void)
|
|
|
|
{
|
|
|
|
CELL top = dpeek();
|
|
|
|
CELL next = get(ds - CELLS);
|
|
|
|
ds += CELLS * 2;
|
|
|
|
put(ds - CELLS,next);
|
|
|
|
put(ds,top);
|
|
|
|
}
|
|
|
|
|
|
|
|
void primitive_3dup(void)
|
|
|
|
{
|
|
|
|
CELL c1 = dpeek();
|
|
|
|
CELL c2 = get(ds - CELLS);
|
|
|
|
CELL c3 = get(ds - CELLS * 2);
|
|
|
|
ds += CELLS * 3;
|
|
|
|
put (ds,c1);
|
|
|
|
put (ds - CELLS,c2);
|
|
|
|
put (ds - CELLS * 2,c3);
|
|
|
|
}
|
|
|
|
|
|
|
|
void primitive_rot(void)
|
|
|
|
{
|
|
|
|
CELL c1 = dpeek();
|
|
|
|
CELL c2 = get(ds - CELLS);
|
|
|
|
CELL c3 = get(ds - CELLS * 2);
|
|
|
|
put(ds,c3);
|
|
|
|
put(ds - CELLS,c1);
|
|
|
|
put(ds - CELLS * 2,c2);
|
|
|
|
}
|
|
|
|
|
|
|
|
void primitive__rot(void)
|
|
|
|
{
|
|
|
|
CELL c1 = dpeek();
|
|
|
|
CELL c2 = get(ds - CELLS);
|
|
|
|
CELL c3 = get(ds - CELLS * 2);
|
|
|
|
put(ds,c2);
|
|
|
|
put(ds - CELLS,c3);
|
|
|
|
put(ds - CELLS * 2,c1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void primitive_dupd(void)
|
|
|
|
{
|
|
|
|
CELL top = dpeek();
|
|
|
|
CELL next = get(ds - CELLS);
|
|
|
|
put(ds,next);
|
|
|
|
put(ds - CELLS,next);
|
|
|
|
dpush(top);
|
|
|
|
}
|
|
|
|
|
|
|
|
void primitive_swapd(void)
|
|
|
|
{
|
|
|
|
CELL top = get(ds - CELLS);
|
|
|
|
CELL next = get(ds - CELLS * 2);
|
|
|
|
put(ds - CELLS,next);
|
|
|
|
put(ds - CELLS * 2,top);
|
|
|
|
}
|
|
|
|
|
|
|
|
void primitive_nip(void)
|
|
|
|
{
|
|
|
|
CELL top = dpop();
|
|
|
|
drepl(top);
|
|
|
|
}
|
|
|
|
|
|
|
|
void primitive_2nip(void)
|
|
|
|
{
|
|
|
|
CELL top = dpeek();
|
|
|
|
ds -= CELLS * 2;
|
|
|
|
drepl(top);
|
|
|
|
}
|
|
|
|
|
|
|
|
void primitive_tuck(void)
|
2004-07-16 02:26:21 -04:00
|
|
|
{
|
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);
|
2005-09-05 03:06:47 -04:00
|
|
|
dpush(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
|
|
|
}
|
|
|
|
|
2005-09-05 03:06:47 -04:00
|
|
|
void primitive_swap(void)
|
|
|
|
{
|
|
|
|
CELL top = dpeek();
|
|
|
|
CELL next = get(ds - CELLS);
|
|
|
|
put(ds,next);
|
|
|
|
put(ds - CELLS,top);
|
|
|
|
}
|
|
|
|
|
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-12-10 21:46:42 -05:00
|
|
|
F_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-12-10 21:46:42 -05:00
|
|
|
F_VECTOR* v = vector(depth);
|
2005-04-09 18:30:46 -04:00
|
|
|
F_ARRAY* a = untag_array_fast(v->array);
|
2004-08-12 17:36:36 -04:00
|
|
|
memcpy(a + 1,(void*)bottom,depth * CELLS);
|
2005-01-27 20:06:10 -05:00
|
|
|
v->top = tag_fixnum(depth);
|
2004-07-16 02:26:21 -04:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
void primitive_datastack(void)
|
|
|
|
{
|
2005-06-16 18:50:49 -04:00
|
|
|
maybe_gc(0);
|
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)
|
|
|
|
{
|
2005-06-16 18:50:49 -04:00
|
|
|
maybe_gc(0);
|
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
|
|
|
}
|
|
|
|
|
2006-02-14 23:23:08 -05:00
|
|
|
/* returns pointer to top of stack */
|
2004-12-10 21:46:42 -05:00
|
|
|
CELL vector_to_stack(F_VECTOR* vector, CELL bottom)
|
2004-07-16 02:26:21 -04:00
|
|
|
{
|
2004-08-12 17:36:36 -04:00
|
|
|
CELL start = bottom;
|
2005-01-27 20:06:10 -05:00
|
|
|
CELL len = untag_fixnum_fast(vector->top) * CELLS;
|
2005-04-09 18:30:46 -04:00
|
|
|
memcpy((void*)start,untag_array_fast(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
|
|
|
}
|