Bsexp impl without additional buffer, s_rotate fix

This commit is contained in:
ProgramSnail 2025-03-09 16:33:56 +03:00
parent 032d249ed4
commit 2e59845cec
3 changed files with 21 additions and 45 deletions

View file

@ -145,7 +145,6 @@ static inline void s_put_nth(size_t n, void *val) {
} }
static inline void s_rotate_n(size_t n) { static inline void s_rotate_n(size_t n) {
s_push_nil();
#ifndef WITH_CHECK #ifndef WITH_CHECK
if ((void **)__gc_stack_top + (aint)n - 1 >= s_top()) { if ((void **)__gc_stack_top + (aint)n - 1 >= s_top()) {
s_failure(&s, "not enough elements in stack"); s_failure(&s, "not enough elements in stack");
@ -156,9 +155,9 @@ static inline void s_rotate_n(size_t n) {
#endif #endif
void *buf = NULL; void *buf = NULL;
for (size_t i = 0; 2 * i < n; ++i) { for (size_t i = 0; 2 * i + 1 < n; ++i) {
buf = ((void **)__gc_stack_top)[n - i]; buf = ((void **)__gc_stack_top)[n - i - 1];
((void **)__gc_stack_top)[n - i] = ((void **)__gc_stack_top)[i]; ((void **)__gc_stack_top)[n - i - 1] = ((void **)__gc_stack_top)[i];
((void **)__gc_stack_top)[i] = buf; ((void **)__gc_stack_top)[i] = buf;
} }
} }

View file

@ -80,7 +80,20 @@ void set_argc_argv(int argc, char **argv) {
#endif #endif
} }
void call_Barray(size_t elem_count, char** ip, void** buffer) { static inline void call_Bsexp(const char* name, size_t args_count) {
#ifdef DEBUG_VERSION
printf("tag hash is %i, n is %i\n", UNBOX(LtagHash((char *)name)),
args_count);
#endif
s_push((void *)LtagHash((char *)name));
s_rotate_n(args_count + 1);
void *sexp = Bsexp((aint *)s_peek(), BOX(args_count + 1));
s_popn(args_count + 1);
s_push(sexp);
}
static inline void call_Barray(size_t elem_count) {
s_rotate_n(elem_count); s_rotate_n(elem_count);
// NOTE: not sure if elems should be added // NOTE: not sure if elems should be added
@ -174,43 +187,7 @@ void run_main(Bytefile* bf, int argc, char **argv) {
// params read from stack // params read from stack
const char *name = ip_read_string(&s.ip); const char *name = ip_read_string(&s.ip);
size_t args_count = ip_read_int(&s.ip); size_t args_count = ip_read_int(&s.ip);
#ifdef DEBUG_VERSION call_Bsexp(name, args_count);
printf("tag hash is %i, n is %i\n", UNBOX(LtagHash((char *)name)),
args_count);
#endif
bool use_new_buffer = (args_count >= BUFFER_SIZE);
void **opr_buffer = (void**)(use_new_buffer
? alloc((args_count + 1) * sizeof(void *))
: buffer);
if (use_new_buffer) {
for (size_t i = 0; i <= args_count; ++i) {
opr_buffer[i] = 0;
push_extra_root(&opr_buffer[i]);
}
}
// 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);
if (use_new_buffer) {
for (size_t i = 0; i <= args_count; ++i) {
pop_extra_root(&opr_buffer[i]);
}
free(opr_buffer);
}
break; break;
} }
@ -353,7 +330,7 @@ void run_main(Bytefile* bf, int argc, char **argv) {
s.is_closure_call, args_sz, locals_sz); s.is_closure_call, args_sz, locals_sz);
#ifndef WITH_CHECK #ifndef WITH_CHECK
if ((void **)__gc_stack_top + (aint)max_additional_stack_sz - 1 <= s.stack) { if ((void **)__gc_stack_top + (aint)max_additional_stack_sz - 1 <= s.stack) {
s_failure(&s, "stack owerflow"); s_failure(&s, "stack overflow");
} }
#endif #endif
break; break;
@ -481,7 +458,7 @@ void run_main(Bytefile* bf, int argc, char **argv) {
#endif #endif
if (builtin_id == BUILTIN_Barray) { if (builtin_id == BUILTIN_Barray) {
call_Barray(args_count, &s.ip, buffer); call_Barray(args_count);
} else { } else {
run_stdlib_func(builtin_id, args_count); run_stdlib_func(builtin_id, args_count);
} }

View file

@ -12,7 +12,7 @@ extern size_t __gc_stack_top, __gc_stack_bottom;
// --- State --- // --- State ---
void init_state(struct State* s, void** stack) { void init_state(struct State* s, void** stack) {
__init(); // __init(); // FIXME, disable gc
s->stack = stack; s->stack = stack;
s->fp = NULL; s->fp = NULL;