diff options
| author | Rose Hogenson <rosehogenson@posteo.net> | 2024-02-04 16:46:20 -0800 |
|---|---|---|
| committer | Rose Hogenson <rosehogenson@posteo.net> | 2024-02-04 16:46:20 -0800 |
| commit | 17face3633686e374aa0859271ccf462a48e60aa (patch) | |
| tree | 6a5482717ef1b8194ccf347a6d8af06d664902cd /bytecode/oper.c | |
| parent | d940187fd8720e0ab3c00e5a6a7ae8f181c7752d (diff) | |
| download | sml-17face3633686e374aa0859271ccf462a48e60aa.tar.zst | |
Rewrite the bytecode interpreter in Rust.
Diffstat (limited to 'bytecode/oper.c')
| -rw-r--r-- | bytecode/oper.c | 97 |
1 files changed, 0 insertions, 97 deletions
diff --git a/bytecode/oper.c b/bytecode/oper.c deleted file mode 100644 index 0bdb8f6..0000000 --- a/bytecode/oper.c +++ /dev/null @@ -1,97 +0,0 @@ -#include <stdbool.h> -#include <stdlib.h> - -#include "byte_slice.h" -#include "encoding.h" -#include "heap.h" -#include "panic.h" -#include "value.h" -#include "value_slice.h" - -#include "oper.h" - -struct st { - size_t i; - value locals[NUM_LOCALS]; - struct heap heap; -}; - -static value read_arg(struct st *s, struct arg arg) -{ - if (arg.constant) { - return arg.constant; - } - return s->locals[arg.local]; -} - -static void oalloc(struct st *s, struct op oper) -{ - s->locals[oper.alloc.out] = from_pointer(alloc(&s->heap, to_int(read_arg(s, oper.alloc.size)))); -} - -static void call(struct st *s) -{ - s->i = to_int(s->locals[0]); -} - -static void poke(struct st *s, struct op oper) -{ - value *p = to_pointer(s->locals[oper.poke.pointer]); - p[oper.poke.offset] = read_arg(s, oper.poke.value); -} - -static void peek(struct st *s, struct op oper) -{ - value *p = to_pointer(read_arg(s, oper.peek.value)); - s->locals[oper.peek.out] = p[oper.peek.offset]; -} - -static void shuf(struct st *s, struct op oper) -{ - s->locals[oper.shuf.out] = read_arg(s, oper.shuf.value); -} - -static void oexit(struct st *s, struct op oper) -{ - exit(to_int(read_arg(s, oper.exit.value))); -} - -static void op(struct st *s, struct op oper) -{ - switch (oper.code) { - case OALLOC: - oalloc(s, oper); - break; - case OCALL: - call(s); - break; - case OPOKE: - poke(s, oper); - break; - case OPEEK: - peek(s, oper); - break; - case OSHUF: - shuf(s, oper); - break; - case OEXIT: - oexit(s, oper); - break; - default: - panicf("Invalid op: %x\n", oper.code); - } -} - -void run(uint8_t_slice prog) -{ - struct st s = { - .i = 0, - .locals = { 0 }, - }; - s.heap = new_heap((value_slice) { .size = NUM_LOCALS, .buf = s.locals }); - while (true) { - struct result oper = parse(uint8_t_slice1(prog, s.i)); - s.i += oper.n; - op(&s, oper.op); - } -} |
