lama_byterun/byterun/include/stack.h

310 lines
7.9 KiB
C
Raw Normal View History

2024-10-31 21:08:48 +03:00
#pragma once
#include "../../runtime/gc.h"
#include "module_manager.h"
2024-10-31 21:08:48 +03:00
#include "runtime_externs.h"
#include "types.h"
#include "utils.h"
#include "stdlib.h"
2024-11-12 02:38:26 +03:00
extern struct State s;
2024-11-12 02:12:28 +03:00
extern size_t __gc_stack_top, __gc_stack_bottom;
2024-11-12 02:38:26 +03:00
static inline void **s_top() {
2024-11-14 00:50:43 +03:00
return (void **)__gc_stack_bottom - s.bf->global_area_size;
}
static inline void **s_peek_unsafe() {
return (void **)(__gc_stack_top + sizeof(void *));
}
static inline void **s_peek() {
void **peek = s_peek_unsafe();
#ifndef WITH_CHECK
if (peek == s_top()) {
s_failure(&s, "peek: empty stack");
}
if (s.fp != NULL && peek == f_locals(s.fp)) {
s_failure(&s, "peek: empty function stack");
}
#endif
return peek;
}
2024-11-12 02:38:26 +03:00
static inline bool s_is_empty() {
if (s_peek_unsafe() == s_top() ||
(s.fp != NULL && s_peek_unsafe() == f_locals(s.fp))) {
return true;
}
return false;
}
2024-10-31 21:08:48 +03:00
2024-11-14 02:10:24 +03:00
static inline void **s_nth(size_t n) {
#ifndef WITH_CHECK
if (s_peek_unsafe() + n >= s_top()) {
2024-11-12 02:38:26 +03:00
s_failure(&s, "not enough elements in stack");
}
if (s.fp != NULL && s_peek_unsafe() + n >= f_locals(s.fp)) {
2024-11-12 02:38:26 +03:00
s_failure(&s, "not enough elements in function stack");
}
#endif
return s_peek_unsafe() + n;
}
static inline aint *s_nth_i(size_t n) { return (aint *)s_nth(n); }
2024-11-12 02:38:26 +03:00
static inline aint *s_peek_i() { return (aint *)s_peek(); }
2024-11-12 02:38:26 +03:00
static inline void s_push(void *val) {
#ifndef WITH_CHECK
if (s_peek_unsafe() == s.stack) {
2024-11-12 02:38:26 +03:00
s_failure(&s, "stack overflow");
}
#endif
2024-11-14 00:50:43 +03:00
*(void **)__gc_stack_top = val;
__gc_stack_top -= sizeof(void *);
}
2024-11-12 02:38:26 +03:00
static inline void s_push_i(aint val) { s_push((void *)val); }
2024-11-12 02:38:26 +03:00
static inline void s_push_nil() { s_push(NULL); }
2024-11-12 02:38:26 +03:00
static inline void s_pushn_nil(size_t n) {
#ifndef WITH_CHECK
if (s_peek_unsafe() + (aint)n - 1 <= s.stack) {
s_failure(&s, "pushn: stack overflow");
2024-11-14 00:59:36 +03:00
}
#endif
for (size_t i = 0; i < n; ++i) {
2024-11-14 00:59:36 +03:00
*(void **)__gc_stack_top = NULL;
__gc_stack_top -= sizeof(void *);
}
}
2024-11-12 02:38:26 +03:00
static inline void *s_pop() {
#ifndef WITH_CHECK
if (s_peek_unsafe() == s_top()) {
s_failure(&s, "pop: empty stack");
}
2024-11-14 00:50:43 +03:00
if (s.fp != NULL && (void **)__gc_stack_top == f_locals(s.fp)) {
s_failure(&s, "pop: empty function stack");
}
#endif
2024-11-14 00:50:43 +03:00
__gc_stack_top += sizeof(void *);
void *value = *(void **)__gc_stack_top;
return value;
}
2024-11-12 02:38:26 +03:00
static inline aint s_pop_i() { return (aint)s_pop(); }
2024-11-12 02:38:26 +03:00
static inline void s_popn(size_t n) {
#ifndef WITH_CHECK
if (s_peek_unsafe() + (aint)n - 1 >= s_top()) {
s_failure(&s, "popn: empty stack");
}
if (s.fp != NULL && s_peek_unsafe() + (aint)n - 1 >= f_locals(s.fp)) {
s_failure(&s, "popn: empty function stack");
}
#endif
2024-11-14 00:59:36 +03:00
__gc_stack_top += n * sizeof(void *);
}
2024-10-31 21:08:48 +03:00
2024-11-14 02:10:24 +03:00
// ------ complex operations ------
static inline void s_swap_tops() {
// NOTE: can be optimized
void *x = s_pop();
void *y = s_pop();
s_push(x);
s_push(y);
}
2024-11-14 02:10:24 +03:00
// 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();
#ifndef WITH_CHECK
if (s_peek_unsafe() + n >= s_top()) {
2024-11-14 02:10:24 +03:00
s_failure(&s, "not enough elements in stack");
}
if (s.fp != NULL && s_peek_unsafe() + n >= f_locals(s.fp)) {
2024-11-14 02:10:24 +03:00
s_failure(&s, "not enough elements in function stack");
}
#endif
2024-11-14 02:10:24 +03:00
for (size_t i = 0; i < n; ++i) {
s_peek_unsafe()[i] = s_peek_unsafe()[i + 1];
2024-11-14 02:10:24 +03:00
}
s_peek_unsafe()[n] = val;
2024-11-14 02:10:24 +03:00
}
static inline void s_rotate_n(size_t n) {
#ifndef WITH_CHECK
if (s_peek_unsafe() + (aint)n - 1 >= s_top()) {
2024-11-14 02:10:24 +03:00
s_failure(&s, "not enough elements in stack");
}
if (s.fp != NULL && s_peek_unsafe() + (aint)n - 1 >= f_locals(s.fp)) {
2024-11-14 02:10:24 +03:00
s_failure(&s, "not enough elements in function stack");
}
#endif
2024-11-14 02:10:24 +03:00
void *buf = NULL;
for (size_t i = 0; 2 * i + 1 < n; ++i) {
buf = s_peek_unsafe()[n - i - 1];
s_peek_unsafe()[n - i - 1] = s_peek_unsafe()[i];
s_peek_unsafe()[i] = buf;
2024-11-14 02:10:24 +03:00
}
}
2024-10-31 21:08:48 +03:00
// ------ functions ------
// |> param_0 ... param_n | frame[ ret rp prev_fp ... &params &locals &end
2024-10-31 21:08:48 +03:00
// ]
// |> local_0 ... local_m |> | ...
//
// where |> defines corresponding frame pointer, | is stack pointer
// location before / after new frame added
static inline void s_enter_f(char *rp, bool is_closure_call, auint args_sz,
auint locals_sz) {
// check that params count is valid
if (s_peek_unsafe() + (aint)args_sz - (is_closure_call ? 0 : 1) >= s_top()) {
2024-11-12 02:38:26 +03:00
s_failure(&s, "not enough parameters in stack");
}
2024-11-12 02:38:26 +03:00
if (s.fp != NULL &&
s_peek_unsafe() + (aint)args_sz - (is_closure_call ? 0 : 1) >=
2024-11-14 00:50:43 +03:00
f_locals(s.fp)) {
2024-11-12 02:38:26 +03:00
s_failure(&s, "not enough parameters in function stack");
}
2024-12-17 04:15:25 +03:00
void *closure = is_closure_call ? *s_nth(args_sz) : NULL;
// printf("is_closure_call: %i, closure: %li\n", is_closure_call,
// (aint)closure);
// s_push_nil(s); // sp contains value, frame starts with next value
2024-11-12 02:38:26 +03:00
s_pushn_nil(frame_sz());
// create frame
struct Frame frame = {
.closure = closure,
.ret = NULL, // field in frame itself
.rp = rp,
2024-11-12 02:38:26 +03:00
.prev_fp = (void **)s.fp,
.args_sz_box = BOX(args_sz),
.locals_sz_box = BOX(locals_sz),
};
// put frame on stack
s.fp = (struct Frame *)s_peek_unsafe();
2024-11-12 02:38:26 +03:00
(*s.fp) = frame;
2024-11-12 02:38:26 +03:00
s_pushn_nil(locals_sz);
}
2024-11-12 02:38:26 +03:00
static inline void s_exit_f() {
2024-12-15 16:19:54 +03:00
#ifndef WITH_CHECK
2024-11-12 02:38:26 +03:00
if (s.fp == NULL) {
s_failure(&s, "exit: no func");
}
2024-12-15 16:19:54 +03:00
#endif
2024-11-12 02:38:26 +03:00
struct Frame frame = *s.fp;
// drop stack entities, locals, frame
size_t to_pop = f_args(s.fp) - s_peek_unsafe();
2024-11-12 02:38:26 +03:00
s.fp = (struct Frame *)f_prev_fp(&frame);
s_popn(to_pop);
// drop args
2024-11-12 02:38:26 +03:00
s_popn(f_args_sz(&frame));
2025-03-16 14:26:48 +03:00
if (frame.closure) {
s_pop();
}
// save returned value, not in main
if (frame.prev_fp != 0) {
2024-11-12 02:38:26 +03:00
s_push(frame.ret);
}
2024-11-12 02:38:26 +03:00
s.ip = frame.rp;
}
2025-01-11 23:51:50 +03:00
static inline void print_stack(struct State *state) {
void **peek = s_peek_unsafe();
printf("stack (%li) is\n[", state->stack + STACK_SIZE - peek);
for (void **x = state->stack + STACK_SIZE - 1; x > peek; --x) {
printf("(%li, ", (long)UNBOX(*x));
printf("%p) ", *x);
}
printf("]\n");
}
2024-10-31 21:08:48 +03:00
// --- category ---
2024-10-31 21:08:48 +03:00
2024-11-14 02:10:24 +03:00
static inline void **var_by_category(enum VarCategory category, size_t id) {
void **var = NULL;
switch (category) {
case VAR_GLOBAL:
#ifndef WITH_CHECK
2024-11-12 02:38:26 +03:00
if (s.bf->global_area_size <= id) {
s_failure(&s,
"can't read global: too big id"); //, %i >= %ul", id,
2024-11-12 02:38:26 +03:00
// s.bf->global_area_size);
}
#endif
var = s.bf->global_ptr - id;
break;
case VAR_LOCAL:
#ifndef WITH_CHECK
2024-11-12 02:38:26 +03:00
if (s.fp == NULL) {
s_failure(&s, "can't read local outside of function");
}
2024-11-12 02:38:26 +03:00
if (f_locals_sz(s.fp) <= id) {
s_failure(&s, "can't read local: too big id"); //, %i >= %ul", id,
// f_locals_sz(s.fp));
}
#endif
// printf("id is %i, local is %i, %i\n", id,
2024-11-14 00:50:43 +03:00
// UNBOX((auint)*((void**)f_locals(s.fp) + id)), f_locals(s.fp) - (void
// **)__gc_stack_top);
2024-11-12 02:38:26 +03:00
var = f_locals(s.fp) + (f_locals_sz(s.fp) - id - 1);
break;
case VAR_ARGUMENT:
#ifndef WITH_CHECK
2024-11-12 02:38:26 +03:00
if (s.fp == NULL) {
s_failure(&s, "can't read argument outside of function");
}
2024-11-12 02:38:26 +03:00
if (f_args_sz(s.fp) <= id) {
s_failure(&s, "can't read arguments: too big id"); //, %i >= %ul", id,
// f_args_sz(s.fp));
}
#endif
2024-11-12 02:38:26 +03:00
var = f_args(s.fp) + (f_args_sz(s.fp) - id - 1);
break;
2024-12-15 16:19:54 +03:00
case VAR_CLOSURE:
#ifndef WITH_CHECK
2024-11-12 02:38:26 +03:00
if (s.fp == NULL) {
s_failure(&s, "can't read closure parameter outside of function");
}
2024-11-12 02:38:26 +03:00
if (s.fp->closure == NULL) {
s_failure(&s, "can't read closure parameter not in closure");
}
2024-12-15 16:19:54 +03:00
#endif
2024-11-12 02:38:26 +03:00
if (UNBOXED(s.fp->closure)) {
s_failure(&s, "not boxed value expected in closure index");
}
2024-11-12 02:38:26 +03:00
data *d = TO_DATA(s.fp->closure);
2024-12-17 04:15:25 +03:00
int count = get_len(d) - 1;
if ((int64_t)id >= count) {
2024-11-12 02:38:26 +03:00
s_failure(&s,
"can't read arguments: too big id"); //, %i >= %ul", id, count);
}
return ((void **)d->contents) + id + 1; // order is not important
}
return var;
}