1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
#include <stdbool.h>
#include <stdlib.h>
#include "encoding.h"
#include "heap.h"
#include "panic.h"
#include "slice.h"
#include "value.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(struct slice prog)
{
struct st s = {
.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;
op(&s, oper.op);
}
}
|