fixes, less allocations

This commit is contained in:
ProgramSnail 2024-11-14 02:10:24 +03:00
parent 1548c16eeb
commit eb6267defe
2 changed files with 54 additions and 16 deletions

View file

@ -23,10 +23,7 @@ static inline bool s_is_empty() {
return false; return false;
} }
static inline void **s_nth(aint n) { static inline void **s_nth(size_t n) {
if (n < 0) {
s_failure(&s, "can't access stack by negative index");
}
if ((void **)__gc_stack_top + n >= s_top()) { if ((void **)__gc_stack_top + n >= s_top()) {
s_failure(&s, "not enough elements in stack"); 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 *); __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 ------ // ------ functions ------
// |> param_0 ... param_n | frame[ ret rp prev_fp &params &locals &end // |> param_0 ... param_n | frame[ ret rp prev_fp &params &locals &end
@ -186,10 +219,7 @@ static inline void print_stack() {
// --- category --- // --- category ---
static inline void **var_by_category(enum VarCategory category, int id) { static inline void **var_by_category(enum VarCategory category, size_t id) {
if (id < 0) {
s_failure(&s, "can't read variable: negative id"); // %i", id);
}
void **var = NULL; void **var = NULL;
switch (category) { switch (category) {
case VAR_GLOBAL: case VAR_GLOBAL:

View file

@ -156,12 +156,16 @@ void run(bytefile *bf, int argc, char **argv) {
? alloc((args_count + 1) * sizeof(void *)) ? alloc((args_count + 1) * sizeof(void *))
: buffer; : buffer;
// s_put_nth(args_count, (void *)LtagHash((char *)name));
for (size_t i = 1; i <= args_count; ++i) { for (size_t i = 1; i <= args_count; ++i) {
opr_buffer[args_count - i] = s_pop(); opr_buffer[args_count - i] = s_pop();
} }
opr_buffer[args_count] = (void *)LtagHash((char *)name); opr_buffer[args_count] = (void *)LtagHash((char *)name);
void *sexp = Bsexp((aint *)opr_buffer, BOX(args_count + 1)); 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); s_push(sexp);
break; break;
@ -440,16 +444,20 @@ void run(bytefile *bf, int argc, char **argv) {
case CMD_BUILTIN_Barray: { // CALL Barray %d case CMD_BUILTIN_Barray: { // CALL Barray %d
size_t elem_count = ip_read_int(&s.ip); size_t elem_count = ip_read_int(&s.ip);
void **opr_buffer = elem_count > BUFFER_SIZE // void **opr_buffer = elem_count > BUFFER_SIZE
? alloc(elem_count * sizeof(void *)) // ? alloc(elem_count * sizeof(void *))
: buffer; // : buffer;
for (size_t i = 0; i < elem_count; ++i) { // for (size_t i = 0; i < elem_count; ++i) {
opr_buffer[elem_count - i - 1] = s_pop(); // 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 = void *array =
Barray((aint *)opr_buffer, Barray((aint *)s_peek(),
BOX(elem_count)); // NOTE: not shure if elems should be added BOX(elem_count));
s_push(array); s_push(array);
break; break;
} }