From 009fdd6bfe32d01c5f1a6ce5dd403715b22f3029 Mon Sep 17 00:00:00 2001 From: ProgramSnail Date: Fri, 29 Nov 2024 23:43:35 +0300 Subject: [PATCH] part of multimodule execution impl in interpreter: separation of execution parts --- byterun/include/interpreter.h | 10 ++++++- byterun/include/types.h | 4 ++- byterun/src/cli.c | 13 +++++++-- byterun/src/interpreter.c | 38 +++++++++++++------------- byterun/src/types.c | 51 +++++++++++++++++++++++++---------- 5 files changed, 78 insertions(+), 38 deletions(-) diff --git a/byterun/include/interpreter.h b/byterun/include/interpreter.h index 2a87ac2fe..90c10d732 100644 --- a/byterun/include/interpreter.h +++ b/byterun/include/interpreter.h @@ -2,4 +2,12 @@ #include "parser.h" -void run(uint mod_id, int argc, char **argv); +void run_init(size_t *stack); + +void run_init_mod_rec(uint mod_id); + +void run_prepare_exec(int argc, char **argv); + +void run_mod(uint mod_id, int argc, char **argv); + +void run_cleanup(); diff --git a/byterun/include/types.h b/byterun/include/types.h index c9067c048..fa2c3ad06 100644 --- a/byterun/include/types.h +++ b/byterun/include/types.h @@ -69,7 +69,9 @@ struct State { char *call_ip; // prev instruction pointer (to remember jmp locations) }; -void construct_state(uint mod_id, struct State *s, void **stack); +void init_state(struct State *s, void **stack); +void init_mod_state(uint mod_id, struct State *s); +void init_mod_state_globals(struct State *s); void cleanup_state(struct State *state); // TODO: print current mod id diff --git a/byterun/src/cli.c b/byterun/src/cli.c index 5ed2a26c5..0c00c1130 100644 --- a/byterun/src/cli.c +++ b/byterun/src/cli.c @@ -1,6 +1,7 @@ #include "interpreter.h" #include "module_manager.h" #include "parser.h" +#include "types.h" #include "utils.h" #include "../../runtime/runtime.h" @@ -9,10 +10,18 @@ int main(int argc, char** argv) { failure("two arguments should be provided: execution option (-i/-p) and file name\n"); } + bytefile *f = read_file(argv[2]); - uint main_mod_id = mod_add(f); if (strcmp(argv[1], "-i") == 0) { - run(main_mod_id, argc - 1, argv + 1); // TODO: init all modules, execute all prev. modules + uint main_mod_id = mod_add(f); + + size_t stack[STACK_SIZE]; + run_init(stack); + + run_init_mod_rec(main_mod_id); + run_prepare_exec(argc - 1, argv + 1); + // TODO: run all included modules before + run_mod(main_mod_id, argc - 1, argv + 1); } else if (strcmp(argv[1], "-p") == 0) { dump_file (stdout, f); } else { diff --git a/byterun/src/interpreter.c b/byterun/src/interpreter.c index 039695837..881e3f72b 100644 --- a/byterun/src/interpreter.c +++ b/byterun/src/interpreter.c @@ -38,23 +38,12 @@ static inline const char *ip_read_string(char **ip) { const size_t BUFFER_SIZE = 1000; -void init_stack(int argc, char **argv) {} // TODO +// size_t stack[STACK_SIZE]; +void run_init(size_t *stack) { + init_state(&s, (void**)stack); +} -void init_mod_rec(uint mod_id) {} // TODO - -void run(uint mod_id, int argc, char **argv) { // TODO: remove mod init and stack init - size_t stack[STACK_SIZE]; - void *buffer[BUFFER_SIZE]; - construct_state( - mod_id, &s, - (void **) - stack); // TODO: separate on each module part and part for all run - -#ifdef DEBUG_VERSION - printf("--- interpreter run ---\n"); -#endif - - // argc, argv +void run_prepare_exec(int argc, char **argv) { { s_push_i(BOX(argc)); for (size_t i = 0; i < argc; ++i) { @@ -65,10 +54,18 @@ void run(uint mod_id, int argc, char **argv) { // TODO: remove mod init and stac s_popn(argc); s_push(argv_elem); } +} -#ifdef DEBUG_VERSION - printf("- loop start\n"); -#endif +void run_init_mod_rec(uint mod_id) { + init_mod_state(mod_id, &s); + init_mod_state_globals(&s); + // TODO: recursive for imports, check if visited +} + +void run_mod(uint mod_id, int argc, char **argv) { + init_mod_state(mod_id, &s); + + void *buffer[BUFFER_SIZE]; do { bool call_happened = false; @@ -505,5 +502,6 @@ stop:; #ifdef DEBUG_VERSION printf("--- run end ---\n"); #endif - cleanup_state(&s); } + +void run_cleanup() { cleanup_state(&s); } diff --git a/byterun/src/types.c b/byterun/src/types.c index b1c211ba4..ac9460c65 100644 --- a/byterun/src/types.c +++ b/byterun/src/types.c @@ -12,12 +12,14 @@ extern size_t __gc_stack_top, __gc_stack_bottom; // --- State --- -static void init_state(uint mod_id, struct State* s, void** stack) { +void init_state(struct State* s, void** stack) { + __init(); + s->stack = stack; - s->bf = mod_get(mod_id); + s->bf = NULL; s->is_closure_call = false; - s->current_module_id = mod_id; - s->call_module_id = 0; // TODO: ?? + s->current_module_id = 0; + s->call_module_id = 0; s->ip = s->bf->code_ptr; s->instr_ip = s->bf->code_ptr; s->call_ip = NULL; @@ -27,27 +29,48 @@ static void init_state(uint mod_id, struct State* s, void** stack) { s->stack[i] = NULL; } - // printf("%p:%zu - %zu", s->stack, (size_t)s->stack, (size_t)s->stack & 0xF); - - // s->sp = s->stack + STACK_SIZE; // [top -> bottom] stack s->fp = NULL; -} -void construct_state(uint mod_id, struct State* s, void** stack) { - __init(); - init_state(mod_id, s, stack); __gc_stack_bottom = (size_t)(s->stack + STACK_SIZE); __gc_stack_top = __gc_stack_bottom; - s_pushn_nil(s->bf->global_area_size); // TODO: move to run, do for each module - s->bf->global_ptr = (void*)__gc_stack_top; - #ifdef DEBUG_VERSION print_stack(s); printf("- state init done\n"); #endif } +void init_mod_state(uint mod_id, struct State* s) { + // init module data + s->bf = mod_get(mod_id); + s->current_module_id = mod_id; + + // clearup from previous executions + + s->is_closure_call = false; + s->current_module_id = 0; + s->call_module_id = 0; + s->call_ip = NULL; + s->current_line = 0; + + s->fp = NULL; + +#ifdef DEBUG_VERSION + print_stack(s); + printf("- mod state init done\n"); +#endif +} + +void init_mod_state_globals(struct State *s) { + s_pushn_nil(s->bf->global_area_size); + s->bf->global_ptr = (void*)__gc_stack_top; + +#ifdef DEBUG_VERSION + print_stack(s); + printf("- state globals init done\n"); +#endif +} + static void destruct_state(struct State* state) { // free(state->stack);