part of multimodule execution impl in interpreter: frame extension, state exstension, call and return

This commit is contained in:
ProgramSnail 2024-11-29 23:11:46 +03:00
parent 0a26953318
commit 59f3dfb43e
9 changed files with 108 additions and 59 deletions

View file

@ -2,4 +2,4 @@
#include "parser.h"
void run(bytefile *bf, int argc, char **argv);
void run(uint mod_id, int argc, char **argv);

View file

@ -5,9 +5,9 @@
#include "utils.h"
struct ModSearchResult {
ssize_t symbol_offset; // < 0 => not found
size_t symbol_offset;
uint32_t mod_id;
bytefile *mod_file;
bytefile *mod_file; // = NULL => not found
};
void mod_add_search_path(const char *path);
@ -16,4 +16,6 @@ bytefile *mod_get(uint32_t id);
int32_t mod_load(const char *name); // < 0 => not found
uint32_t mod_add(bytefile *module);
struct ModSearchResult mod_search_pub_symbol(const char *name);

View file

@ -1,6 +1,7 @@
#pragma once
#include "../../runtime/gc.h"
#include "module_manager.h"
#include "runtime_externs.h"
#include "types.h"
#include "utils.h"
@ -134,14 +135,14 @@ static inline void s_rotate_n(size_t n) {
// ------ functions ------
// |> param_0 ... param_n | frame[ ret rp prev_fp &params &locals &end
// |> param_0 ... param_n | frame[ ret rp prev_fp ... &params &locals &end
// ]
// |> local_0 ... local_m |> | ...
//
// where |> defines corresponding frame pointer, | is stack pointer
// location before / after new frame added
static inline void s_enter_f(char *rp, bool is_closure_call, auint args_sz,
auint locals_sz) {
static inline void s_enter_f(char *rp, aint ret_module_id, bool is_closure_call,
auint args_sz, auint locals_sz) {
#ifdef DEBUG_VERSION
printf("-> %i args sz\n", args_sz);
printf("-> %i locals sz\n", locals_sz);
@ -169,6 +170,7 @@ static inline void s_enter_f(char *rp, bool is_closure_call, auint args_sz,
.ret = NULL, // field in frame itself
.rp = rp,
.prev_fp = (void **)s.fp,
.ret_module_box = BOX(ret_module_id),
.args_sz_box = BOX(args_sz),
.locals_sz_box = BOX(locals_sz),
};
@ -207,6 +209,10 @@ static inline void s_exit_f() {
}
s.ip = frame.rp;
s.current_module_id = UNBOX(frame.ret_module_box);
s.bf = mod_get(s.current_module_id);
// TODO: return to ret_module
}
static inline void print_stack() {

View file

@ -22,13 +22,14 @@ static const size_t MAX_ARRAY_SIZE = 0x11111110;
// ------ Frame ------
struct Frame {
void *closure; // where closure value stored if needed
void *ret; // store returned value [gc pointer]
char *rp; // ret instruction pointer [not gc pointer]
void **prev_fp; // ret function frame pointer [boxed value, not gc
// pointer]
aint args_sz_box; // store arguments [boxed value, not gc pointer]
aint locals_sz_box; // store locals [boxed value, not gc pointer]
void *closure; // where closure value stored if needed
void *ret; // store returned value [gc pointer]
char *rp; // ret instruction pointer [not gc pointer]
void **prev_fp; // ret function frame pointer [boxed value, not gc
// pointer]
aint ret_module_box; // module to return [boxed value, not gc pointer]
aint args_sz_box; // store arguments [boxed value, not gc pointer]
aint locals_sz_box; // store locals [boxed value, not gc pointer]
};
// NOTE: stack is [top -> bottom]
@ -60,14 +61,18 @@ struct State {
bool is_closure_call;
uint current_module_id;
uint call_module_id;
char *ip; // instruction pointer
char *instr_ip; // poiter to current instruction
char *call_ip; // prev instruction pointer (to remember jmp locations)
};
void construct_state(bytefile *bf, struct State *s, void **stack);
void construct_state(uint mod_id, struct State *s, void **stack);
void cleanup_state(struct State *state);
// TODO: print current mod id
static inline void s_failure(struct State *s, const char *msg) {
exec_failure(read_cmd(s->instr_ip), s->current_line,
s->instr_ip - s->bf->code_ptr, msg);