This commit is contained in:
ProgramSnail 2024-11-07 19:07:26 +03:00
parent ce013b682c
commit 8800fd3b1d
4 changed files with 32 additions and 16 deletions

View file

@ -11,9 +11,11 @@ int main(int argc, char** argv) {
failure("too many arguments");
}
// printf("size of aint is %i\n", sizeof(aint));
bytefile *f = read_file(argv[1]);
#ifdef DEBUG_VERSION
dump_file (stdout, f);
#endif
run(f);
free(f);

View file

@ -22,9 +22,10 @@ char *ip_read_string(char **ip, bytefile *bf) {
void run(bytefile *bf) {
struct State s;
init_state(bf, &s);
print_stack(&s);
#ifdef DEBUG_VERSION
printf("--- interpreter run ---\n");
#endif
const size_t OPS_SIZE = 13;
const char *ops[] = {
@ -54,7 +55,9 @@ void run(bytefile *bf) {
const char *argv_0 = "interpreter";
s_push(&s, Bstring((aint *)&argv_0)); // argv
#ifdef DEBUG_VERSION
printf("- loop start\n");
#endif
do {
// char *before_op_ip = s.ip; // save to set s.prev_ip
@ -62,7 +65,9 @@ void run(bytefile *bf) {
char x = ip_read_byte(&s.ip), h = (x & 0xF0) >> 4, l = x & 0x0F;
#ifdef DEBUG_VERSION
printf("0x%.8x\n", s.ip - bf->code_ptr - 1);
#endif
switch (h) {
case 15:
@ -98,8 +103,10 @@ void run(bytefile *bf) {
// params read from stack
const char *name = ip_read_string(&s.ip, bf);
aint args_count = ip_read_int(&s.ip);
#ifdef DEBUG_VERSION
printf("tag hash is %i, n os %i\n", UNBOX(LtagHash((char *)name)),
args_count);
#endif
if (args_count < 0) {
failure("SEXP: args count should be >= 0");
@ -321,19 +328,10 @@ void run(bytefile *bf) {
const char *name = ip_read_string(&s.ip, bf);
aint args_count = ip_read_int(&s.ip);
#ifdef DEBUG_VERSION
printf("tag hash is %i, n is %i, peek is %i\n",
UNBOX(LtagHash((char *)name)), args_count, s_peek(&s));
if (UNBOXED(s_peek(&s)))
printf("aaaaaaaaaaaaaaaaaaa");
else {
data *r = TO_DATA(s_peek(&s));
if ((aint)BOX(TAG(r->data_header) != SEXP_TAG))
printf("bbbbbbbb %i %i", TAG(r->data_header), SEXP_TAG);
if (TO_SEXP(s_peek(&s))->tag != UNBOX(LtagHash((char *)name)))
printf("cccccccccc");
if (LEN(r->data_header) != UNBOX(args_count))
printf("dddddd %i %i", LEN(r->data_header), UNBOX(args_count));
}
#endif
s_push_i(&s, Btag(s_pop(&s), LtagHash((char *)name), args_count));
break;
@ -434,9 +432,13 @@ void run(bytefile *bf) {
if (s.fp == NULL) {
break;
}
#ifdef DEBUG_VERSION
print_stack(&s);
#endif
} while (1);
stop:;
#ifdef DEBUG_VERSION
printf("--- run end ---\n");
#endif
cleanup_state(&s);
}

View file

@ -78,7 +78,9 @@ void s_push(struct State *s, void *val) {
if (s->sp == s->stack) {
failure("stack overflow");
}
#ifdef DEBUG_VERSION
printf("--> push\n");
#endif
--s->sp;
*s->sp = val;
}
@ -104,7 +106,9 @@ void* s_pop(struct State *s) {
if (s->fp != NULL && s->sp == f_locals(s->fp)) {
failure("empty function stack");
}
#ifdef DEBUG_VERSION
printf("--> pop\n");
#endif
void* value = *s->sp;
*s->sp = NULL;
++s->sp;
@ -125,8 +129,10 @@ void s_popn(struct State *s, size_t n) {
// ------ functions ------
void s_enter_f(struct State *s, char *rp, bool is_closure_call, auint args_sz, auint locals_sz) {
#ifdef DEBUG_VERSION
printf("-> %i args sz\n", args_sz);
printf("-> %i locals sz\n", locals_sz);
#endif
// check that params count is valid
if (s->sp + (aint)args_sz - (is_closure_call ? 0 : 1) >= s_top(s)) {
@ -169,11 +175,15 @@ void s_exit_f(struct State *s) {
// drop stack entities, locals, frame
size_t to_pop = f_args(s->fp) - s->sp;
s->fp = (struct Frame*)f_prev_fp(&frame);
#ifdef DEBUG_VERSION
printf("-> %zu to pop\n", to_pop);
#endif
s_popn(s, to_pop);
// drop args
#ifdef DEBUG_VERSION
printf("-> + %zu to pop\n", f_args_sz(&frame));
#endif
s_popn(s, f_args_sz(&frame));
// save returned value, not in main
@ -238,7 +248,9 @@ void **var_by_category(struct State *s, enum VarCategory category,
if (UNBOXED(s->fp->closure)) { ASSERT_BOXED(".elem:1", s->fp->closure); }
data* d = TO_DATA(s->fp->closure);
size_t count = get_len(d) - 1;
#ifdef DEBUG_VERSION
printf("id is %i, count is %i\n", id, count);
#endif
if (count <= id) {
failure("can't read arguments: too big id, %i >= %ul", id, count);
}

View file

@ -36,7 +36,6 @@ static void alloc_state(bytefile *bf, struct State* s) {
}
s->sp = s->stack + STACK_SIZE; // [top -> bottom] stack
print_stack(s);
s->fp = NULL;
}
@ -44,12 +43,13 @@ void init_state(bytefile *bf, struct State* s) {
__init();
alloc_state(bf, s);
__gc_stack_bottom = (size_t)s->sp;
// print_stack(s);
s_pushn_nil(s, bf->global_area_size);
// print_stack(&state);
#ifdef DEBUG_VERSION
print_stack(s);
printf("- state init done\n");
#endif
}
static void destruct_state(struct State* state) {