diff --git a/byterun/include/stack.h b/byterun/include/stack.h index 9ffe4a794..8412790ac 100644 --- a/byterun/include/stack.h +++ b/byterun/include/stack.h @@ -328,7 +328,7 @@ static inline void **var_by_category(enum VarCategory category, size_t id) { s_failure(&s, "can't read arguments: too big id"); //, %i >= %ul", id, count); } - return ((void **)d->contents) + count - id; // order is not important + return ((void **)d->contents) + id + 1; // order is not important } return var; diff --git a/byterun/src/interpreter.c b/byterun/src/interpreter.c index c47c3e4a1..432881936 100644 --- a/byterun/src/interpreter.c +++ b/byterun/src/interpreter.c @@ -395,6 +395,7 @@ void run_main(Bytefile* bf, int argc, char **argv) { s_failure(&s, "closure"); } #endif + s_rotate_n(args_count); // NOTE: call_offset < 0 => deal with closure of builtin function s_push_i(BOX(call_offset)); diff --git a/byterun/src/module_manager.cpp b/byterun/src/module_manager.cpp index 763c98747..5d992438e 100644 --- a/byterun/src/module_manager.cpp +++ b/byterun/src/module_manager.cpp @@ -12,32 +12,33 @@ extern "C" { #include #include -#include #include #include #include -template +template requires(N == 0) void call_func(void (*f)(), size_t n, Args... args) { - // std::cout << std::endl << "args_count=" << n << " "; // TODO FIXME TMP - std::cout << std::endl; // TODO: TMP, to work with problematic \n display asm volatile("movq %0, %%r11" : /* no outputs */ : "m"(n)); - /*s_push(*/ ((void (*)(Args...))f)(args...); //); - s_push(0); + if constexpr (return_value) { + s_push(((void *(*)(Args...))f)(args...)); + } else { + ((void (*)(Args...))f)(args...); + s_push(0); + } } -template +template requires(N != 0) void call_func(void (*f)(), size_t n, Args... args) { void *arg = s_pop(); - call_func(f, n, arg, args...); + call_func(f, n, arg, args...); // TODO: check that arg is added on the right position } -template +template void call_anyarg_func(void (*f)(), size_t n) { if constexpr (do_check) { if (n > N) { @@ -45,9 +46,9 @@ void call_anyarg_func(void (*f)(), size_t n) { } } if (n == N) { - call_func(f, n); + call_func(f, n); } else if constexpr (N > 0) { - call_anyarg_func(f, n); + call_anyarg_func(f, n); } } @@ -591,7 +592,7 @@ void run_stdlib_func(BUILTIN id, size_t args_count) { break; case BUILTIN_Lassert: // NOTE: basic params: .args_count = 2, .is_vararg = true - call_anyarg_func<20>((void (*)()) & Lassert, args_count); + call_anyarg_func<20, false>((void (*)()) & Lassert, args_count); break; case BUILTIN_Lstring: ret = Lstring(s_nth_i(0)); // .is_args = true @@ -641,7 +642,7 @@ void run_stdlib_func(BUILTIN id, size_t args_count) { break; case BUILTIN_Lsprintf: // NOTE: basic params: .args_count = 1, .is_vararg = true - call_anyarg_func<20>((void (*)()) & Lsprintf, args_count); + call_anyarg_func<20, true>((void (*)()) & Lsprintf, args_count); break; case BUILTIN_Lsubstring: // std::cout << "substr\n"; @@ -707,7 +708,7 @@ void run_stdlib_func(BUILTIN id, size_t args_count) { break; case BUILTIN_Lprintf: // NOTE: basic params: .args_count = 1, .is_vararg = true - call_anyarg_func<20>((void (*)()) & Lprintf, args_count); + call_anyarg_func<20, false>((void (*)()) & Lprintf, args_count); break; case BUILTIN_Lfopen: ret = (void *)Lfopen((char *)*s_nth(1), (char *)*s_nth(0)); @@ -736,7 +737,7 @@ void run_stdlib_func(BUILTIN id, size_t args_count) { break; case BUILTIN_Lfprintf: // NOTE: basic params: .args_count = 2, .is_vararg = true - call_anyarg_func<20>((void (*)()) & Lfprintf, args_count); + call_anyarg_func<20, false>((void (*)()) & Lfprintf, args_count); break; case BUILTIN_Lregexp: ret = (void *)Lregexp((char *)*s_nth(0)); @@ -751,7 +752,7 @@ void run_stdlib_func(BUILTIN id, size_t args_count) { break; case BUILTIN_Lfailure: // NOTE: basic params: .args_count = 1, .is_vararg = true - call_anyarg_func<20>((void (*)()) & Lfailure, args_count); + call_anyarg_func<20, false>((void (*)()) & Lfailure, args_count); break; case BUILTIN_Lsystem: ret = (void *)Lsystem((char *)*s_nth(0)); diff --git a/byterun/stdlib_regression_check.sh b/byterun/stdlib_regression_check.sh index da68e1e54..673f5965f 100755 --- a/byterun/stdlib_regression_check.sh +++ b/byterun/stdlib_regression_check.sh @@ -31,11 +31,16 @@ for test in ../stdlib/regression/*.lama; do # echo "" | ./byterun.exe -p $test_file.bc # echo "" | ./byterun.exe -vi $test_file.bc echo "" | ./byterun.exe -vi $test_file.bc > test.log - # sed '1d;s/^..//' $test_file.t > test_orig.log - # diff test.log test_orig.log + sed '1d;2d;3d;s/^..//' $test_path.t > test_orig.log + # echo "Result:" + # cat test.log + # echo "Original:" + # cat test_orig.log + echo "Diff:" + diff test.log test_orig.log rm $test_file.bc - # rm test.log test_orig.log + rm test.log test_orig.log echo "done" done