-
Notifications
You must be signed in to change notification settings - Fork 1
/
interpreter.c
58 lines (56 loc) · 1.69 KB
/
interpreter.c
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
#include "interpreter.h"
#include "opcodes.h"
#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
typedef union _cell_t {
uintptr_t u;
intptr_t i;
union _cell_t *a;
unsigned char *b;
uint16_t *s;
uint32_t *l;
uint8_t *ip;
void *v;
union _cell_t (*f0)(void);
union _cell_t (*f1)(union _cell_t);
union _cell_t (*f2)(union _cell_t, union _cell_t);
union _cell_t (*f3)(union _cell_t, union _cell_t, union _cell_t);
union _cell_t (*f4)(union _cell_t, union _cell_t, union _cell_t,
union _cell_t);
union _cell_t (*f5)(union _cell_t, union _cell_t, union _cell_t,
union _cell_t, union _cell_t);
union _cell_t (*f6)(union _cell_t, union _cell_t, union _cell_t,
union _cell_t, union _cell_t, union _cell_t);
union _cell_t (*f7)(union _cell_t, union _cell_t, union _cell_t,
union _cell_t, union _cell_t, union _cell_t,
union _cell_t);
union _cell_t (*f8)(union _cell_t, union _cell_t, union _cell_t,
union _cell_t, union _cell_t, union _cell_t,
union _cell_t, union _cell_t);
} cell_t;
void interpret(void *code, void *dstack, void *rstack, void *user) {
cell_t tos, *sp = dstack, *rp = rstack, *up = user, t;
uint8_t *ip = code, op;
tos = *sp--;
for (;;) {
op = *ip++;
#if 0
#define V(num, _, name, _junk) \
if (op == num) { \
fprintf(stderr, "%d:%s\n", op, name); \
}
INSTRUCTIONS(V)
#undef V
#endif
switch (op) {
#define V(num, _, _junk, code) case num: code; break;
INSTRUCTIONS(V)
#undef V
default:
fprintf(stderr, "Bad opcode!!! %d\n", op);
exit(1);
break;
}
}
}