diff --git a/vm/contexts.cpp b/vm/contexts.cpp index b0a27ef18f..86156de3b5 100644 --- a/vm/contexts.cpp +++ b/vm/contexts.cpp @@ -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(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(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(); +} + } diff --git a/vm/primitives.hpp b/vm/primitives.hpp index f534a24791..c7534ec1cc 100644 --- a/vm/primitives.hpp +++ b/vm/primitives.hpp @@ -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 } diff --git a/vm/vm.hpp b/vm/vm.hpp index 6c76cdddfa..73bcffc764 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -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;