From 60622614731bc42dd44704d3cff55b4e4c4f492c Mon Sep 17 00:00:00 2001 From: Rose Hogenson Date: Sat, 11 Feb 2023 13:40:10 -0800 Subject: Write a garbage collector. Does it work? No, but it compiles. --- bytecode/oper.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'bytecode/oper.c') diff --git a/bytecode/oper.c b/bytecode/oper.c index ed4a681..fcb7d77 100644 --- a/bytecode/oper.c +++ b/bytecode/oper.c @@ -2,6 +2,7 @@ #include #include "encoding.h" +#include "heap.h" #include "panic.h" #include "slice.h" #include "value.h" @@ -10,7 +11,8 @@ struct st { size_t i; - value locals[8]; + value locals[NUM_LOCALS]; + struct heap heap; }; static value read_arg(struct st *s, struct arg arg) @@ -21,9 +23,9 @@ static value read_arg(struct st *s, struct arg arg) return s->locals[arg.local]; } -static void alloc(struct st *s, struct op oper) +static void oalloc(struct st *s, struct op oper) { - s->locals[oper.alloc.out] = from_pointer(calloc(to_int(read_arg(s, oper.alloc.size)), sizeof(value))); + s->locals[oper.alloc.out] = from_pointer(alloc(&s->heap, to_int(read_arg(s, oper.alloc.size)))); } static void call(struct st *s) @@ -57,7 +59,7 @@ static void op(struct st *s, struct op oper) { switch (oper.code) { case OALLOC: - alloc(s, oper); + oalloc(s, oper); break; case OCALL: call(s); @@ -85,6 +87,7 @@ void run(struct slice prog) .i = 0, .locals = { 0 }, }; + s.heap = new_heap((struct val_slice) { .size = NUM_LOCALS, .buf = s.locals }); while (true) { struct result oper = parse(slice1(prog, s.i)); s.i += oper.n; -- cgit v1.3.1