moved contexts functions into vm

db4
Phil Dawes 2009-08-17 21:37:04 +01:00
parent adf2529451
commit 88084a66ac
3 changed files with 117 additions and 17 deletions

View File

@ -8,27 +8,42 @@ namespace factor
cell ds_size, rs_size;
context *unused_contexts;
void reset_datastack()
void factorvm::reset_datastack()
{
ds = ds_bot - sizeof(cell);
}
void reset_retainstack()
void reset_datastack()
{
return vm->reset_datastack();
}
void factorvm::reset_retainstack()
{
rs = rs_bot - sizeof(cell);
}
void reset_retainstack()
{
return vm->reset_retainstack();
}
static const cell stack_reserved = (64 * sizeof(cell));
void fix_stacks()
void factorvm::fix_stacks()
{
if(ds + sizeof(cell) < ds_bot || ds + stack_reserved >= ds_top) reset_datastack();
if(rs + sizeof(cell) < rs_bot || rs + stack_reserved >= rs_top) reset_retainstack();
}
void fix_stacks()
{
return vm->fix_stacks();
}
/* called before entry into foreign C code. Note that ds and rs might
be stored in registers, so callbacks must save and restore the correct values */
void save_stacks()
void factorvm::save_stacks()
{
if(stack_chain)
{
@ -37,7 +52,12 @@ void save_stacks()
}
}
context *alloc_context()
void save_stacks()
{
return vm->save_stacks();
}
context *factorvm::alloc_context()
{
context *new_context;
@ -56,14 +76,24 @@ context *alloc_context()
return new_context;
}
void dealloc_context(context *old_context)
context *alloc_context()
{
return vm->alloc_context();
}
void factorvm::dealloc_context(context *old_context)
{
old_context->next = unused_contexts;
unused_contexts = old_context;
}
void dealloc_context(context *old_context)
{
return vm->dealloc_context(old_context);
}
/* called on entry into a compiled callback */
void nest_stacks()
void factorvm::nest_stacks()
{
context *new_context = alloc_context();
@ -94,8 +124,13 @@ void nest_stacks()
reset_retainstack();
}
void nest_stacks()
{
return vm->nest_stacks();
}
/* called when leaving a compiled callback */
void unnest_stacks()
void factorvm::unnest_stacks()
{
ds = stack_chain->datastack_save;
rs = stack_chain->retainstack_save;
@ -109,8 +144,13 @@ void unnest_stacks()
dealloc_context(old_stacks);
}
void unnest_stacks()
{
return vm->unnest_stacks();
}
/* called on startup */
void init_stacks(cell ds_size_, cell rs_size_)
void factorvm::init_stacks(cell ds_size_, cell rs_size_)
{
ds_size = ds_size_;
rs_size = rs_size_;
@ -118,7 +158,12 @@ void init_stacks(cell ds_size_, cell rs_size_)
unused_contexts = NULL;
}
bool stack_to_array(cell bottom, cell top)
void init_stacks(cell ds_size_, cell rs_size_)
{
return vm->init_stacks(ds_size_,rs_size_);
}
bool factorvm::stack_to_array(cell bottom, cell top)
{
fixnum depth = (fixnum)(top - bottom + sizeof(cell));
@ -133,38 +178,68 @@ bool stack_to_array(cell bottom, cell top)
}
}
PRIMITIVE(datastack)
bool stack_to_array(cell bottom, cell top)
{
return vm->stack_to_array(bottom,top);
}
inline void factorvm::vmprim_datastack()
{
if(!stack_to_array(ds_bot,ds))
general_error(ERROR_DS_UNDERFLOW,F,F,NULL);
}
PRIMITIVE(retainstack)
PRIMITIVE(datastack)
{
PRIMITIVE_GETVM()->vmprim_datastack();
}
inline void factorvm::vmprim_retainstack()
{
if(!stack_to_array(rs_bot,rs))
general_error(ERROR_RS_UNDERFLOW,F,F,NULL);
}
PRIMITIVE(retainstack)
{
PRIMITIVE_GETVM()->vmprim_retainstack();
}
/* returns pointer to top of stack */
cell array_to_stack(array *array, cell bottom)
cell factorvm::array_to_stack(array *array, cell bottom)
{
cell depth = array_capacity(array) * sizeof(cell);
memcpy((void*)bottom,array + 1,depth);
return bottom + depth - sizeof(cell);
}
PRIMITIVE(set_datastack)
cell array_to_stack(array *array, cell bottom)
{
return vm->array_to_stack(array,bottom);
}
inline void factorvm::vmprim_set_datastack()
{
ds = array_to_stack(untag_check<array>(dpop()),ds_bot);
}
PRIMITIVE(set_retainstack)
PRIMITIVE(set_datastack)
{
PRIMITIVE_GETVM()->vmprim_set_datastack();
}
inline void factorvm::vmprim_set_retainstack()
{
rs = array_to_stack(untag_check<array>(dpop()),rs_bot);
}
PRIMITIVE(set_retainstack)
{
PRIMITIVE_GETVM()->vmprim_set_retainstack();
}
/* Used to implement call( */
PRIMITIVE(check_datastack)
inline void factorvm::vmprim_check_datastack()
{
fixnum out = to_fixnum(dpop());
fixnum in = to_fixnum(dpop());
@ -189,4 +264,9 @@ PRIMITIVE(check_datastack)
}
}
PRIMITIVE(check_datastack)
{
PRIMITIVE_GETVM()->vmprim_check_datastack();
}
}

View File

@ -5,5 +5,5 @@ extern "C" typedef void (*primitive_type)();
extern const primitive_type primitives[];
#define PRIMITIVE(name) extern "C" void primitive_##name()
#define PRIMITIVE_GETVM() vm->
#define PRIMITIVE_GETVM() vm
}

View File

@ -2,6 +2,26 @@ namespace factor
{
struct factorvm {
// contexts
void reset_datastack();
void reset_retainstack();
void fix_stacks();
void save_stacks();
context *alloc_context();
void dealloc_context(context *old_context);
void nest_stacks();
void unnest_stacks();
void init_stacks(cell ds_size_, cell rs_size_);
bool stack_to_array(cell bottom, cell top);
cell array_to_stack(array *array, cell bottom);
inline void vmprim_datastack();
inline void vmprim_retainstack();
inline void vmprim_set_datastack();
inline void vmprim_set_retainstack();
inline void vmprim_check_datastack();
// next method here:
};
extern factorvm *vm;