new module system fixes, fine result on regression tests (same to before mod)

This commit is contained in:
ProgramSnail 2025-03-03 00:13:19 +03:00
parent 7ab5944536
commit 25322bd3d7
6 changed files with 44 additions and 24 deletions

View file

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

View file

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

View file

@ -320,7 +320,6 @@ void analyze(Bytefile *bf, std::vector<size_t> &&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

View file

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

View file

@ -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<Bytefile *> &&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;

View file

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