part of multimodule execution impl in interpreter: separation of execution parts

This commit is contained in:
ProgramSnail 2024-11-29 23:43:35 +03:00
parent 59f3dfb43e
commit 009fdd6bfe
5 changed files with 78 additions and 38 deletions

View file

@ -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();

View file

@ -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

View file

@ -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 {

View file

@ -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); }

View file

@ -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);