diff --git a/byterun/include/stack.h b/byterun/include/stack.h index 31bd8b507..be7ad2545 100644 --- a/byterun/include/stack.h +++ b/byterun/include/stack.h @@ -7,6 +7,8 @@ #include "stdlib.h" +extern size_t __gc_stack_top, __gc_stack_bottom; + static inline void **s_top(struct State *s) { return s->stack + STACK_SIZE - s->bf->global_area_size; } @@ -54,7 +56,8 @@ static inline void s_push(struct State *s, void *val) { #endif --s->sp; *s->sp = val; - // __gc_stack_top= (size_t)(s->sp - 1); + __gc_stack_top = (size_t)(s->sp); + __gc_stack_top -= __gc_stack_top & 0xF; } static inline void s_push_i(struct State *s, aint val) { @@ -82,7 +85,8 @@ static inline void *s_pop(struct State *s) { void *value = *s->sp; *s->sp = NULL; ++s->sp; - // __gc_stack_top = (size_t)(s->sp - 1); + __gc_stack_top = (size_t)(s->sp); + __gc_stack_top -= __gc_stack_top & 0xF; return value; } @@ -226,7 +230,7 @@ static inline void **var_by_category(struct State *s, enum VarCategory category, if (s->fp->closure == NULL) { s_failure(s, "can't read closure parameter not in closure"); } - if (!UNBOXED(s->fp->closure)) { // TODO: check ?? + if (UNBOXED(s->fp->closure)) { s_failure(s, "not boxed value expected in closure index"); } data *d = TO_DATA(s->fp->closure); diff --git a/byterun/include/types.h b/byterun/include/types.h index ffc2acbd5..af97fdc44 100644 --- a/byterun/include/types.h +++ b/byterun/include/types.h @@ -15,7 +15,7 @@ // CLOJURE_T = CLOSURE_TAG, // }; -#define STACK_SIZE 100000 +#define STACK_SIZE 128 * 1024 static const size_t MAX_ARRAY_SIZE = 0x11111110; diff --git a/byterun/src/interpreter.c b/byterun/src/interpreter.c index c041a53b7..248fc566b 100644 --- a/byterun/src/interpreter.c +++ b/byterun/src/interpreter.c @@ -19,8 +19,10 @@ char *ip_read_string(char **ip, bytefile *bf) { return get_string(bf, ip_read_int(ip)); } +const size_t BUFFER_SIZE = 1000; + void run(bytefile *bf, int argc, char **argv) { - void *buffer[10000]; // FIXME: TODO: TMP + void *buffer[BUFFER_SIZE]; struct State s; init_state(bf, &s); @@ -118,20 +120,22 @@ void run(bytefile *bf, int argc, char **argv) { if (args_count < 0) { s_failure(&s, "args count should be >= 0"); } - // void **buffer = calloc(args_count + 1, sizeof(void *)); + void **opr_buffer = args_count >= BUFFER_SIZE ? calloc(args_count + 1, sizeof(void *)) : buffer; for (size_t i = 1; i <= args_count; ++i) { - buffer[args_count - i] = s_pop(&s); + opr_buffer[args_count - i] = s_pop(&s); } - buffer[args_count] = (void *)LtagHash((char *)name); + opr_buffer[args_count] = (void *)LtagHash((char *)name); - void *sexp = Bsexp((aint *)buffer, BOX(args_count + 1)); + void *sexp = Bsexp((aint *)opr_buffer, BOX(args_count + 1)); push_extra_root(sexp); s_push(&s, sexp); pop_extra_root(sexp); - // free(buffer); + if (args_count >= BUFFER_SIZE) { + free(opr_buffer); + } break; } @@ -426,17 +430,18 @@ void run(bytefile *bf, int argc, char **argv) { s_failure(&s, "elements count should be >= 0"); } - // void **buffer = calloc(elem_count, sizeof(void *)); + void **opr_buffer = elem_count > BUFFER_SIZE ? calloc(elem_count, sizeof(void *)) : buffer; for (size_t i = 0; i < elem_count; ++i) { - buffer[elem_count - i - 1] = s_pop(&s); + opr_buffer[elem_count - i - 1] = s_pop(&s); } void *array = - Barray((aint *)buffer, BOX(elem_count)); // NOTE: not shure if elems should be added - // s_popn(&s, elem_count); + Barray((aint *)opr_buffer, BOX(elem_count)); // NOTE: not shure if elems should be added s_push(&s, array); - // free(buffer); + if (elem_count > BUFFER_SIZE) { + free(opr_buffer); + } break; }