From 25322bd3d78950fe636a8a1d9cc12f5dfbc5f5ba Mon Sep 17 00:00:00 2001 From: ProgramSnail Date: Mon, 3 Mar 2025 00:13:19 +0300 Subject: [PATCH] new module system fixes, fine result on regression tests (same to before mod) --- byterun/include/stack.h | 28 +++++++++++++++++++++------- byterun/regression_check.sh | 6 +++--- byterun/src/analyzer.cpp | 1 - byterun/src/interpreter.c | 16 ++++++++-------- byterun/src/module_manager.cpp | 15 +++++++++++---- byterun/src/types.c | 2 +- 6 files changed, 44 insertions(+), 24 deletions(-) diff --git a/byterun/include/stack.h b/byterun/include/stack.h index 005d821c9..99ca283f3 100644 --- a/byterun/include/stack.h +++ b/byterun/include/stack.h @@ -40,12 +40,15 @@ static inline void **s_nth(size_t n) { static inline void **s_peek() { #ifndef WITH_CHECK if ((void **)__gc_stack_top == s_top()) { - s_failure(&s, "empty stack"); + s_failure(&s, "peek: empty stack"); } if (s.fp != NULL && (void **)__gc_stack_top == f_locals(s.fp)) { - s_failure(&s, "empty function stack"); + s_failure(&s, "peek: empty function stack"); } #endif +#ifdef DEBUG_VERSION + printf("--> peek\n"); +#endif return (void **)__gc_stack_top; } @@ -72,8 +75,11 @@ static inline void s_push_nil() { s_push(NULL); } static inline void s_pushn_nil(size_t n) { #ifndef WITH_CHECK if ((void **)__gc_stack_top + (aint)n - 1 <= s.stack) { - s_failure(&s, "stack overflow"); + s_failure(&s, "pushn: stack overflow"); } +#endif +#ifdef DEBUG_VERSION + printf("--> push %zu\n", n); #endif for (size_t i = 0; i < n; ++i) { __gc_stack_top -= sizeof(void *); @@ -84,10 +90,10 @@ static inline void s_pushn_nil(size_t n) { static inline void *s_pop() { #ifndef WITH_CHECK if ((void **)__gc_stack_top == s_top()) { - s_failure(&s, "empty stack"); + s_failure(&s, "pop: empty stack"); } if (s.fp != NULL && (void **)__gc_stack_top == f_locals(s.fp)) { - s_failure(&s, "empty function stack"); + s_failure(&s, "pop: empty function stack"); } #endif #ifdef DEBUG_VERSION @@ -101,9 +107,17 @@ static inline void *s_pop() { static inline aint s_pop_i() { return (aint)s_pop(); } static inline void s_popn(size_t n) { +#ifndef WITH_CHECK if ((void **)__gc_stack_top + (aint)n - 1 >= s_top()) { - s_failure(&s, "empty stack"); + s_failure(&s, "popn: empty stack"); } + if (s.fp != NULL && (void **)__gc_stack_top + (aint)n - 1 >= f_locals(s.fp)) { + s_failure(&s, "popn: empty function stack"); + } +#endif +#ifdef DEBUG_VERSION + printf("--> popn %zu\n", n); +#endif __gc_stack_top += n * sizeof(void *); } @@ -251,7 +265,7 @@ static inline void **var_by_category(enum VarCategory category, size_t id) { // s.bf->global_area_size); } #endif - var = s.bf->global_ptr + STACK_SIZE - 1 - id; + var = s.bf->global_ptr - id; break; case VAR_LOCAL: #ifndef WITH_CHECK diff --git a/byterun/regression_check.sh b/byterun/regression_check.sh index 162a7d908..31ac79a44 100755 --- a/byterun/regression_check.sh +++ b/byterun/regression_check.sh @@ -10,13 +10,13 @@ compiler=../_build/default/src/Driver.exe echo "Used compiler path:" echo $compiler -for test in ../regression/*009.lama; do +for test in ../regression/*.lama; do echo $test $compiler -b $test > /dev/null test_file="${test%.*}" echo $test_file - cat $test_file.input | ./byterun.exe -p test*.bc #> /dev/null - cat $test_file.input | ./byterun.exe -vi test*.bc #> /dev/null + cat $test_file.input | ./byterun.exe -p test*.bc > /dev/null + cat $test_file.input | ./byterun.exe -vi test*.bc > /dev/null rm test*.bc echo "done" done diff --git a/byterun/src/analyzer.cpp b/byterun/src/analyzer.cpp index 1a278658a..8617c6f57 100644 --- a/byterun/src/analyzer.cpp +++ b/byterun/src/analyzer.cpp @@ -320,7 +320,6 @@ void analyze(Bytefile *bf, std::vector &&add_publics) { case Cmd::LINE: break; case Cmd::BUILTIN: { - std::cout << "builtin\n"; // TODO: find link to real function and replace call (need to save all // modules in one space) <- optimization diff --git a/byterun/src/interpreter.c b/byterun/src/interpreter.c index bc897a0e1..49fd3c45d 100644 --- a/byterun/src/interpreter.c +++ b/byterun/src/interpreter.c @@ -147,9 +147,9 @@ void run_main(Bytefile* bf, int argc, char **argv) { s.instr_ip = s.ip; uint8_t x = ip_read_byte(&s.ip), h = (x & 0xF0) >> 4, l = x & 0x0F; -// #ifdef DEBUG_VERSION +#ifdef DEBUG_VERSION printf("0x%.8x: %s\n", s.ip - s.bf->code_ptr - 1, read_cmd(s.ip - 1, s.bf)); -// #endif +#endif switch (h) { case CMD_EXIT: @@ -371,7 +371,7 @@ void run_main(Bytefile* bf, int argc, char **argv) { // #else // uint locals_sz = ip_read_int(&s.ip); // #endif -#ifdef WITH_CHECK +#ifndef WITH_CHECK if (s.fp != NULL && s.call_ip == NULL) { s_failure(&s, "begin should only be called after call"); } @@ -402,7 +402,7 @@ void run_main(Bytefile* bf, int argc, char **argv) { #endif s_enter_f(s.call_ip /*ip from call*/, s.is_closure_call, args_sz, locals_sz); -#ifdef WITH_CHECK +#ifndef WITH_CHECK if ((void **)__gc_stack_top + (aint)max_additional_stack_sz - 1 <= s.stack) { s_failure(&s, "stack overflow"); } @@ -498,20 +498,20 @@ void run_main(Bytefile* bf, int argc, char **argv) { size_t builtin_id = ip_read_int(&s.ip); size_t args_count = ip_read_int(&s.ip); // args count +#ifdef DEBUG_VERSION printf("builtin id: %zu\n", builtin_id); -// #ifndef WITH_CHECK +#endif +#ifndef WITH_CHECK if (builtin_id >= BUILTIN_NONE) { s_failure(&s, "invalid builtin"); } -// #endif +#endif if (builtin_id == BUILTIN_Barray) { call_Barray(args_count, &s.ip, buffer); } else { run_stdlib_func(builtin_id, args_count); } - printf("builtin end\n"); - fflush(stdout); break; } diff --git a/byterun/src/module_manager.cpp b/byterun/src/module_manager.cpp index 419800f07..c01271b41 100644 --- a/byterun/src/module_manager.cpp +++ b/byterun/src/module_manager.cpp @@ -60,7 +60,14 @@ void rewrite_code_with_offsets(Bytefile *bytefile, const Offsets &offsets) { char *ip = bytefile->code_ptr; while (ip - bytefile->code_ptr < bytefile->code_size) { char *instr_ip = ip; + +#ifdef DEBUG_VERSION + std::cout << ip - bytefile->code_ptr << ": "; + const auto [cmd, l] = parse_command(&ip, bytefile, std::cout); + std::cout << '\n'; +#else const auto [cmd, l] = parse_command(&ip, bytefile); +#endif char *read_ip = instr_ip + 1; char *write_ip = instr_ip + 1; @@ -91,8 +98,8 @@ void rewrite_code_with_offsets(Bytefile *bytefile, const Offsets &offsets) { break; } case Cmd::LD: + case Cmd::LDA: case Cmd::ST: - case Cmd::STA: if (to_var_category(l) == VAR_GLOBAL) { ip_write_int_unsafe(write_ip, ip_read_int_unsafe(&read_ip) + offsets.globals); @@ -248,6 +255,7 @@ MergeResult merge_files(std::vector &&bytefiles) { offsets.globals += bytefiles[i]->global_area_size; offsets.code += bytefiles[i]->code_size; offsets.publics_num += bytefiles[i]->public_symbols_number; + free(bytefiles[i]); } @@ -330,7 +338,6 @@ void mod_load_rec(Bytefile *mod, } loaded.insert({import_str, import_mod}); mod_load_rec(import_mod, loaded, loaded_ord); - // loaded_ord.push_back(import_mod); } } loaded_ord.push_back(mod); @@ -344,9 +351,9 @@ MergeResult load_with_imports(Bytefile *root, bool do_verification) { MergeResult result = merge_files(std::move(loaded_ord)); if (do_verification) { - // #ifdef DEBUG_VERSION +#ifdef DEBUG_VERSION printf("main offsets count: %zu\n", result.main_offsets.size()); - // #endif +#endif analyze(result.bf /*, std::move(result.main_offsets)*/); } return result; diff --git a/byterun/src/types.c b/byterun/src/types.c index 1a38e7f49..814fa926b 100644 --- a/byterun/src/types.c +++ b/byterun/src/types.c @@ -58,8 +58,8 @@ void prepare_state(Bytefile* bf, struct State* s) { } void push_globals(struct State *s) { - s_pushn_nil(s->bf->global_area_size); s->bf->global_ptr = (void*)__gc_stack_top; + s_pushn_nil(s->bf->global_area_size); #ifdef DEBUG_VERSION print_stack(s);