Began work on garbage collector.

This commit is contained in:
Jacob Signorovitch 2025-05-24 10:37:29 -04:00
parent 1d83aa65a4
commit 0fb1f1d55f
8 changed files with 139 additions and 13 deletions

View File

@ -1,7 +1,9 @@
#include <inttypes.h>
#include <stdio.h> #include <stdio.h>
#include "include/ast.h" #include "include/ast.h"
#include "include/dstr.h" #include "include/dstr.h"
#include "include/gc.h"
#include "include/scope.h" #include "include/scope.h"
#include "include/util.h" #include "include/util.h"
@ -19,7 +21,7 @@ static char* asttype_names[] = {
}; };
AST* ast_init(ASTType type, void* data) { AST* ast_init(ASTType type, void* data) {
AST* ast = malloc(sizeof(AST)); AST* ast = gc_alloc(sizeof(AST), GC_TYPE_AST);
ast->type = type; ast->type = type;
ast->data = data; ast->data = data;
@ -62,6 +64,23 @@ void ast_destroy(AST* ast) {
free(ast); free(ast);
} }
void ast_destroy_psv(AST* ast) {
if (!ast) return;
switch (ast->type) {
case AST_TYPE_NUM: ast_num_data_destroy(ast->data); break;
case AST_TYPE_CALL: ast_call_data_destroy_psv(ast->data); break;
case AST_TYPE_VREF: ast_vref_data_destroy(ast->data); break;
case AST_TYPE_VDEF: ast_vdef_data_destroy_psv(ast->data); break;
case AST_TYPE_BLOCK: ast_block_data_destroy_psv(ast->data); break;
case AST_TYPE_FDEF: ast_fdef_data_destroy_psv(ast->data); break;
case AST_TYPE_ARG: ast_arg_data_destroy(ast->data); break;
case AST_TYPE_BIF: ast_bif_data_destroy(ast->data); break;
default:
log_dbgf("Unknown ast type %d (max: %d)", ast->type, AST_TYPE_MAX);
}
}
void ast_print(AST* ast) { ast_print_i(ast, 0); } void ast_print(AST* ast) { ast_print_i(ast, 0); }
void ast_print_i(AST* ast, int i) { void ast_print_i(AST* ast, int i) {
@ -132,6 +151,8 @@ ASTBIFData* ast_bif_data_init(AST* fn(size_t, AST**, Scope*)) {
return (ASTBIFData*)fn; return (ASTBIFData*)fn;
} }
void ast_bif_data_destroy(ASTBIFData* bif) { return; }
ASTCallData* ast_call_data_init(char* to, size_t argc, AST** argv) { ASTCallData* ast_call_data_init(char* to, size_t argc, AST** argv) {
talloc(ASTCallData, call); talloc(ASTCallData, call);
@ -152,6 +173,13 @@ void ast_call_data_destroy(ASTCallData* call) {
free(call); free(call);
} }
void ast_call_data_destroy_psv(ASTCallData* call) {
if (!call) return;
free(call->to);
free(call->argv);
free(call);
}
void ast_call_print(ASTCallData* data, int i) { void ast_call_print(ASTCallData* data, int i) {
INDENT_BEGIN(i); INDENT_BEGIN(i);
@ -178,6 +206,11 @@ void ast_vdef_data_destroy(ASTVDefData* vdef) {
free(vdef); free(vdef);
} }
void ast_vdef_data_destroy_psv(ASTVDefData* vdef) {
free(vdef->name);
free(vdef);
}
void ast_vdef_print(ASTVDefData* vdef, int depth) { void ast_vdef_print(ASTVDefData* vdef, int depth) {
INDENT_BEGIN(depth); INDENT_BEGIN(depth);
@ -228,6 +261,11 @@ void ast_block_data_destroy(ASTBlockData* block) {
free(block); free(block);
} }
void ast_block_data_destroy_psv(ASTBlockData* block) {
free(block->inside);
free(block);
}
void ast_block_print(ASTBlockData* data, int depth) { void ast_block_print(ASTBlockData* data, int depth) {
INDENT_BEGIN(depth); INDENT_BEGIN(depth);
@ -256,6 +294,13 @@ void ast_fdef_data_destroy(ASTFDefData* fdef) {
ast_destroy(fdef->body); ast_destroy(fdef->body);
} }
void ast_fdef_data_destroy_psv(ASTFDefData* fdef) {
free(fdef->name);
free(fdef->argv);
free(fdef->body);
free(fdef);
}
void ast_fdef_print(ASTFDefData* fdef, int i) { void ast_fdef_print(ASTFDefData* fdef, int i) {
INDENT_BEGIN(i) INDENT_BEGIN(i)
INDENT_TITLE("ASTFDefData", fdef); INDENT_TITLE("ASTFDefData", fdef);

View File

@ -42,7 +42,7 @@ AST* exec_start(AST* ast) {
dlist_destroy(builtins); dlist_destroy(builtins);
*/ */
scope_destroy_psv(global); // scope_destroy_psv(global);
return res; return res;
} }

37
src/gc.c Normal file
View File

@ -0,0 +1,37 @@
#include "include/gc.h"
#include "include/ast.h"
#include "include/scope.h"
#include "include/util.h"
#include <stdio.h>
#include <stddef.h>
GC* gclist = NULL;
void gc_destroy(GC* gc) { free(gc); }
void* gc_alloc(size_t sz, GCType type) {
void* mem = malloc(sz);
GC* gc = malloc(sizeof(GC));
gc->p = mem;
gc->type = type;
gc->marked = false;
gc->nxt = gclist;
gclist = gc;
return mem;
}
void gc_hack_free() {
while (gclist) {
log_dbgf("freeing %p", gclist);
GC* gc = gclist;
gclist = gclist->nxt;
switch (gc->type) {
case GC_TYPE_AST: ast_destroy_psv(gc->p); break;
case GC_TYPE_SCOPE: scope_destroy_psv(gc->p); break;
}
gc_destroy(gc);
}
}

View File

@ -41,6 +41,8 @@ AST* ast_init(ASTType type, void* data);
AST* ast_init_scope(ASTType type, void* data, Scope* scope); AST* ast_init_scope(ASTType type, void* data, Scope* scope);
// Destroy an `AST`, recursively. // Destroy an `AST`, recursively.
void ast_destroy(AST* ast); void ast_destroy(AST* ast);
// Destroy an `AST`.
void ast_destroy_psv(AST* ast);
// Print an `AST`, recursively. // Print an `AST`, recursively.
void ast_print(AST* ast); void ast_print(AST* ast);
// Helper function to `ast_print()`, where `i` is indentation level. // Helper function to `ast_print()`, where `i` is indentation level.
@ -73,8 +75,8 @@ typedef AST* (*ASTBIFData)(size_t argc, AST** argv, Scope* scope);
// Create a built-in function. // Create a built-in function.
ASTBIFData* ast_bif_data_init(AST* fn(size_t, AST**, Scope*)); ASTBIFData* ast_bif_data_init(AST* fn(size_t, AST**, Scope*));
// Destroy an `ASTBIFData`.
// There is no `ASTBIFData` destroy function, as function pointers are immortal. void ast_bif_data_destroy(ASTBIFData* bif);
// A call (to a function). // A call (to a function).
typedef struct { typedef struct {
@ -85,8 +87,10 @@ typedef struct {
// Create a new `ASTCallData`. // Create a new `ASTCallData`.
ASTCallData* ast_call_data_init(char* to, size_t argc, AST** argv); ASTCallData* ast_call_data_init(char* to, size_t argc, AST** argv);
// Destroy an `ASTCallData`. // Destroy an `ASTCallData` recursively.
void ast_call_data_destroy(ASTCallData* call); void ast_call_data_destroy(ASTCallData* call);
// Destroy an `ASTCallData`.
void ast_call_data_destroy_psv(ASTCallData *call);
// Print an `ASTCallData`. // Print an `ASTCallData`.
void ast_call_print(ASTCallData*, int i); void ast_call_print(ASTCallData*, int i);
@ -100,6 +104,8 @@ typedef struct {
ASTVDefData* ast_vdef_data_init(char* name, AST* val); ASTVDefData* ast_vdef_data_init(char* name, AST* val);
// Destroys the `ASTVDefData`, `ASTVDefData->name`, and `ASTVDefData->val`. // Destroys the `ASTVDefData`, `ASTVDefData->name`, and `ASTVDefData->val`.
void ast_vdef_data_destroy(ASTVDefData* vdef); void ast_vdef_data_destroy(ASTVDefData* vdef);
// Destroy an `ASTVDefData`.
void ast_vdef_data_destroy_psv(ASTVDefData* vdef);
// Print an `ASTVDefData`. // Print an `ASTVDefData`.
void ast_vdef_print(ASTVDefData*, int depth); void ast_vdef_print(ASTVDefData*, int depth);
@ -125,6 +131,8 @@ typedef struct {
ASTBlockData* ast_block_data_init(AST** inside, size_t ln); ASTBlockData* ast_block_data_init(AST** inside, size_t ln);
// Destroy an `ASTBlockData`, recursively. // Destroy an `ASTBlockData`, recursively.
void ast_block_data_destroy(ASTBlockData* block); void ast_block_data_destroy(ASTBlockData* block);
// Destroy an `ASTBlockData`.
void ast_block_data_destroy_psv(ASTBlockData *block);
// Print an `ASTBlockData`. // Print an `ASTBlockData`.
void ast_block_print(ASTBlockData*, int i); void ast_block_print(ASTBlockData*, int i);
@ -137,8 +145,10 @@ typedef struct {
// Create a new `ASTFDefData`. // Create a new `ASTFDefData`.
ASTFDefData* ast_fdef_data_init(char* name, size_t argc, AST** argv, AST* body); ASTFDefData* ast_fdef_data_init(char* name, size_t argc, AST** argv, AST* body);
// Destroy an `ASTFDefData`. // Destroy an `ASTFDefData`, recursively.
void ast_fdef_data_destroy(ASTFDefData* fdef); void ast_fdef_data_destroy(ASTFDefData* fdef);
// Destroy an `ASTFDefData`.
void ast_fdef_data_destroy_psv(ASTFDefData* fdef);
// Print an `ASTFDefData`. // Print an `ASTFDefData`.
void ast_fdef_print(ASTFDefData* fdef, int i); void ast_fdef_print(ASTFDefData* fdef, int i);

31
src/include/gc.h Normal file
View File

@ -0,0 +1,31 @@
#ifndef GC_H
#define GC_H
#include <stdlib.h>
// The type a GC can refer to.
typedef enum {
GC_TYPE_AST,
GC_TYPE_SCOPE,
GC_TYPE_MAX = GC_TYPE_SCOPE
} GCType;
// Added to each AST and Scope; keep track of what's actually still accessible.
typedef struct GC_STRUCT {
void* p; // Pointer to the data.
struct GC_STRUCT* nxt; // The next GC in the linked list.
GCType type; // What type of data.
bool marked; // Whether the data is still accessible.
} GC;
GC* gc_init(void* p, GCType type, GC*);
// Does not free ->p or ->nxt.
void gc_destroy(GC* gc);
// Allocate for an object in the heap, and keep track of it in the GC.
void* gc_alloc(size_t sz, GCType type);
// Free everything, immediately.
void gc_hack_free();
#endif

View File

@ -7,7 +7,7 @@
typedef struct SCOPE_T { typedef struct SCOPE_T {
HTab* here; // This scope's hash table. HTab* here; // This scope's hash table.
struct SCOPE_T* inherit; // The scope to inherit from. struct SCOPE_T* inherit; // The scope to inherit from.
int uses; // How many `AST`s are linked to this scope. int uses; // How many `AST`s are linked to this scope. // TODO: REMOVE.
} Scope; } Scope;
// Create a new `Scope`. Creates new empty `HTab` for current scope. // Create a new `Scope`. Creates new empty `HTab` for current scope.

View File

@ -4,7 +4,7 @@
#include "include/ast.h" #include "include/ast.h"
#include "include/dstr.h" #include "include/dstr.h"
#include "include/exec.h" #include "include/exec.h"
#include "include/gc.h"
#include "include/lexer.h" #include "include/lexer.h"
#include "include/util.h" #include "include/util.h"
@ -22,8 +22,9 @@ int main(int argc, char** argv) {
ast_print(root); ast_print(root);
AST* eval = exec_start(root); AST* eval = exec_start(root);
ast_print(eval); ast_print(eval);
ast_destroy(eval); // ast_destroy(eval);
ast_destroy(root); // ast_destroy(root);
gc_hack_free();
exit(0); exit(0);
} }
@ -60,8 +61,9 @@ int main(int argc, char** argv) {
AST* eval = exec_start(root); AST* eval = exec_start(root);
ast_print(eval); ast_print(eval);
ast_destroy(eval); // ast_destroy(eval);
ast_destroy(root); // ast_destroy(root);
gc_hack_free();
} }
dstr_destroy(ln); dstr_destroy(ln);

View File

@ -1,11 +1,12 @@
#include "include/scope.h" #include "include/scope.h"
#include "include/gc.h"
#include "include/htab.h" #include "include/htab.h"
#include "include/util.h" #include "include/util.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
Scope* scope_init(Scope* inherit) { Scope* scope_init(Scope* inherit) {
Scope* scope = malloc(sizeof(Scope)); Scope* scope = gc_alloc(sizeof(Scope), GC_TYPE_SCOPE);
scope->here = htab_init(); scope->here = htab_init();
scope->inherit = inherit; scope->inherit = inherit;