From eb6267defe657c220dfa1f198f3656922000e0b7 Mon Sep 17 00:00:00 2001 From: ProgramSnail Date: Thu, 14 Nov 2024 02:10:24 +0300 Subject: [PATCH] fixes, less allocations --- byterun/include/stack.h | 46 ++++++++++++++++++++++++++++++++------- byterun/src/interpreter.c | 24 +++++++++++++------- 2 files changed, 54 insertions(+), 16 deletions(-) diff --git a/byterun/include/stack.h b/byterun/include/stack.h index e6b9f0c6f..b58de8ac5 100644 --- a/byterun/include/stack.h +++ b/byterun/include/stack.h @@ -23,10 +23,7 @@ static inline bool s_is_empty() { return false; } -static inline void **s_nth(aint n) { - if (n < 0) { - s_failure(&s, "can't access stack by negative index"); - } +static inline void **s_nth(size_t n) { if ((void **)__gc_stack_top + n >= s_top()) { s_failure(&s, "not enough elements in stack"); } @@ -99,6 +96,42 @@ static inline void s_popn(size_t n) { __gc_stack_top += n * sizeof(void *); } +// ------ complex operations ------ + +// for some reason does not work in sexp constructor, probably connected with gc +// behaviour +static inline void s_put_nth(size_t n, void *val) { + s_push_nil(); + if ((void **)__gc_stack_top + n >= s_top()) { + s_failure(&s, "not enough elements in stack"); + } + if (s.fp != NULL && (void **)__gc_stack_top + n >= f_locals(s.fp)) { + s_failure(&s, "not enough elements in function stack"); + } + + for (size_t i = 0; i < n; ++i) { + ((void **)__gc_stack_top)[i] = ((void **)__gc_stack_top)[i + 1]; + } + ((void **)__gc_stack_top)[n] = val; +} + +static inline void s_rotate_n(size_t n) { + s_push_nil(); + if ((void **)__gc_stack_top + (aint)n - 1 >= s_top()) { + s_failure(&s, "not enough elements in stack"); + } + if (s.fp != NULL && (void **)__gc_stack_top + (aint)n - 1 >= f_locals(s.fp)) { + s_failure(&s, "not enough elements in function stack"); + } + + void *buf = NULL; + for (size_t i = 0; 2 * i < n; ++i) { + buf = ((void **)__gc_stack_top)[n - i]; + ((void **)__gc_stack_top)[n - i] = ((void **)__gc_stack_top)[i]; + ((void **)__gc_stack_top)[i] = buf; + } +} + // ------ functions ------ // |> param_0 ... param_n | frame[ ret rp prev_fp ¶ms &locals &end @@ -186,10 +219,7 @@ static inline void print_stack() { // --- category --- -static inline void **var_by_category(enum VarCategory category, int id) { - if (id < 0) { - s_failure(&s, "can't read variable: negative id"); // %i", id); - } +static inline void **var_by_category(enum VarCategory category, size_t id) { void **var = NULL; switch (category) { case VAR_GLOBAL: diff --git a/byterun/src/interpreter.c b/byterun/src/interpreter.c index 9ed9df2b5..c6d088235 100644 --- a/byterun/src/interpreter.c +++ b/byterun/src/interpreter.c @@ -156,12 +156,16 @@ void run(bytefile *bf, int argc, char **argv) { ? alloc((args_count + 1) * sizeof(void *)) : buffer; + // s_put_nth(args_count, (void *)LtagHash((char *)name)); + for (size_t i = 1; i <= args_count; ++i) { opr_buffer[args_count - i] = s_pop(); } opr_buffer[args_count] = (void *)LtagHash((char *)name); void *sexp = Bsexp((aint *)opr_buffer, BOX(args_count + 1)); + // void *sexp = Bsexp((aint *)s_peek(), BOX(args_count + 1)); + // s_popn(args_count + 1); s_push(sexp); break; @@ -440,16 +444,20 @@ void run(bytefile *bf, int argc, char **argv) { case CMD_BUILTIN_Barray: { // CALL Barray %d size_t elem_count = ip_read_int(&s.ip); - void **opr_buffer = elem_count > BUFFER_SIZE - ? alloc(elem_count * sizeof(void *)) - : buffer; - for (size_t i = 0; i < elem_count; ++i) { - opr_buffer[elem_count - i - 1] = s_pop(); - } + // void **opr_buffer = elem_count > BUFFER_SIZE + // ? alloc(elem_count * sizeof(void *)) + // : buffer; + // for (size_t i = 0; i < elem_count; ++i) { + // opr_buffer[elem_count - i - 1] = s_pop(); + // } + s_rotate_n(elem_count); + // void *array = + // Barray((aint *)opr_buffer, + // BOX(elem_count)); // NOTE: not shure if elems should be added void *array = - Barray((aint *)opr_buffer, - BOX(elem_count)); // NOTE: not shure if elems should be added + Barray((aint *)s_peek(), + BOX(elem_count)); s_push(array); break; }