summaryrefslogtreecommitdiffstats
path: root/bytecode/oper.c
diff options
context:
space:
mode:
authorRose Hogenson <rhogenson@posteo.net>2023-02-11 13:40:10 -0800
committerRose Hogenson <rhogenson@posteo.net>2023-02-11 13:40:10 -0800
commit60622614731bc42dd44704d3cff55b4e4c4f492c (patch)
treedcf3b31f46c1b46e23d5c379a9201195ccb4f1ce /bytecode/oper.c
parent5aec138c3832a65bad4b2bdaa7ca36cf5c279ac8 (diff)
downloadsml-60622614731bc42dd44704d3cff55b4e4c4f492c.tar.zst
Write a garbage collector.
Does it work? No, but it compiles.
Diffstat (limited to 'bytecode/oper.c')
-rw-r--r--bytecode/oper.c11
1 files changed, 7 insertions, 4 deletions
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 <stdlib.h>
#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;