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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>
#include "val.h"
#include "vm.h"
#include "dis.h"
void *M(void *p, size_t sz) {
if (sz == 0) {
free(p);
return NULL;
} else {
return realloc(p, sz);
}
}
Chunk chunk_new() {
return (Chunk){
.blen = 0, .bcap = 0, .b = NULL,
.clen = 0, .ccap = 0, .c = NULL,
};
}
size_t chunk_wbc(Chunk *ch, uint8_t byte) {
if (ch->blen == ch->bcap) {
size_t newsz = (ch->bcap == 0 ? 8 : ch->bcap * 2);
ch->b = M(ch->b, newsz * sizeof(uint8_t));
ch->bcap = newsz;
}
size_t ix = ch->blen;
ch->b[ix] = byte;
ch->blen ++;
return ix;
}
size_t chunk_wconst(Chunk *ch, Val v) {
if (ch->clen == ch->ccap) {
size_t newsz = (ch->ccap == 0 ? 8 : ch->ccap *2);
ch->c = M(ch->c, newsz * sizeof(Val));
ch->ccap = newsz;
}
size_t ix = ch->clen;
ch->c[ix] = v;
ch->clen ++;
return ix;
}
Vm vm_new(Chunk *ch) {
Vm vm = (Vm){
.ch = ch,
.ip = 0,
.sp = 0
};
for (int i = 0; i < STACKSIZE; i++) {
vm.stack[i] = VAL_NIL;
}
return vm;
}
static void runvm() {
Chunk ch = chunk_new();
chunk_wbc(&ch, OP_LOADK);
chunk_wbc(&ch, chunk_wconst(&ch, VAL_NUM(10.0)));
chunk_wbc(&ch, OP_LOADK);
chunk_wbc(&ch, chunk_wconst(&ch, VAL_NUM(3.0)));
chunk_wbc(&ch, OP_DIV);
chunk_wbc(&ch, OP_PRINT);
chunk_wbc(&ch, OP_RET);
disasm_chunk(&ch);
Vm vm = vm_new(&ch);
#define RBYTE() (vm.ch->b[vm.ip++])
#define PUSH(v) vm.stack[vm.sp++] = v;
#define POP() (vm.stack[--vm.sp])
puts("---");
while (1) {
uint8_t instr = RBYTE();
switch (instr) {
case OP_RET:
printf("done!\n");
goto done;
break;
case OP_LOADK:;
uint8_t cix = RBYTE();
Val v = vm.ch->c[cix];
PUSH(v);
// printf(" (pushing ");
// print_val(v);
// printf(")\n");
break;
case OP_PRINT:
println_val(POP());
break;
#define ARITH_OP(opcode, OP) \
case opcode: { \
Val b = POP(); \
Val a = POP(); \
if (!IS_NUM(a) || !IS_NUM(b)) { \
printf("can't do arithmetic on %s and %s\n", \
valty_str(a.ty), valty_str(b.ty)); \
goto done; \
} \
PUSH(VAL_NUM(AS_NUM(a) OP AS_NUM(b))); \
} \
break;
ARITH_OP(OP_ADD, +)
ARITH_OP(OP_SUB, -)
ARITH_OP(OP_MUL, *)
ARITH_OP(OP_DIV, /)
#undef ARITH_OP
}
}
done:;
}
int main() { runvm(); }
|