From b1eda2b742d6ab3893504f2df1541a09988c38c4 Mon Sep 17 00:00:00 2001 From: Tadashi Saito Date: Sun, 17 Nov 2024 15:22:58 +0900 Subject: [PATCH] WIP of stack more. --- gc.c | 45 ++++++++++++++++++++++++++++++++++++++++++--- schaf.c | 1 + 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/gc.c b/gc.c index e701bb6..1230842 100644 --- a/gc.c +++ b/gc.c @@ -154,7 +154,47 @@ static bool in_heap_range(uintptr_t v) if (v < sizeof(Header)) // cannot get Header* return false; const void *p = get_header(v); - return p >= heap && p < heap + init_size; + bool in = p >= heap && p < heap + init_size; + const uintptr_t d = 0x100; + if (!in && (p >= heap - d && p <= (heap + init_size + d))) + debug("ptr? %p", p); + return in; +} + +static bool is_valueish(uintptr_t v) +{ + return value_is_immediate(v) || v == Qnil || v % 8 == 0; +} + +static bool is_valueish_in_heap(uintptr_t v) +{ + ValueTag t = VALUE_TAG((Value) v); + switch (t) { + case TAG_PAIR: { + Pair *p = PAIR(v); + return is_valueish(p->car) && is_valueish(p->cdr); + } + case TAG_CLOSURE: { + Closure *p = CLOSURE(v); + return is_valueish(p->env) && is_valueish(p->params) && + is_valueish(p->body); + } + case TAG_CONTINUATION: { + Continuation *p = CONTINUATION(v); + return is_valueish(p->call_stack) && is_valueish(p->retval); + } + case TAG_STR: { + String *p = STRING(v); + return (uintptr_t) p->body % 8U == 0; + } + case TAG_CFUNC: + case TAG_SYNTAX: { + Procedure *p = PROCEDURE(v); + return p->arity >= -1 && p->arity <= 7; + } + default: + return false; + } } static bool in_heap(uintptr_t v) @@ -162,8 +202,7 @@ static bool in_heap(uintptr_t v) if (value_is_immediate(v) || v == Qnil || v % 8U != 0 || !in_heap_range(v)) return false; - ValueTag t = VALUE_TAG((Value) v); - return t >= TAG_PAIR && t <= TAG_SYNTAX; // need to be precise more? + return is_valueish_in_heap(v); } static void mark_maybe(uintptr_t v) diff --git a/schaf.c b/schaf.c index 0e68884..5c55549 100644 --- a/schaf.c +++ b/schaf.c @@ -1147,6 +1147,7 @@ static Value iload(FILE *in, const char *filename) dump_stack_trace(); return Qundef; } + gc_add_root(&ast); call_stack = Qnil; Value ret = eval_body(&toplevel_environment, l); call_stack_check_consistency();