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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
|
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include "val.h"
#include "vm.h"
#include "mem.h"
#include "dis.h"
#include "com.h"
#include "util.h"
Thread thread_new(State *S) {
Thread th = (Thread){ 0 };
for (int i = 0; i < STACKSIZE; i++) {
th.stack[i] = VAL_NIL;
}
return th;
}
int runvm(State *S) {
Thread *th = S->th;
Chunk *ch = th->ch;
if (S->do_disasm) {
disasm_chunk(ch);
puts("---");
}
#define RBYTE() (ch->bc.d[th->ip++])
#define RSHORT() (th->ip += 2, (uint16_t)( ch->bc.d[th->ip-2] | ch->bc.d[th->ip-1] << 8 ))
#define PUSH(v) th->stack[th->sp++] = v;
#define POP() (th->stack[--th->sp])
#define PEEK() (th->stack[th->sp-1])
// 1 is TOS
#define PEEKN(n) (th->stack[th->sp-n])
while (1) {
ch = th->ch;
if (S->do_trace) {
printf("\t[%lu -> %lu] : ",th->fp, th->sp);
for (int i = th->fp; i < th->sp; i++) {
printf("(%d) ",i);
print_val(th->stack[i]);
printf(" ; ");
}
printf("\n%p ",(void *)ch);
disasm_instr(ch, th->ip);
}
uint8_t instr = RBYTE();
switch (instr) {
case OP_LOADK: {
uint8_t cix = RBYTE();
Val v = ch->consts.d[cix];
PUSH(v);
// printf(" (pushing ");
// print_val(v);
// printf(")\n");
break;
}
case OP_DROP:
--th->sp;
break;
case OP_SKIP: {
uint16_t offset = RSHORT();
th->ip += offset;
break;
}
case OP_REDO: {
uint16_t offset = RSHORT();
th->ip -= offset;
break;
}
case OP_0BRANCH: {
uint16_t offset = RSHORT();
bool cond = is_truthy(POP());
if (!cond)
th->ip += offset;
break;
}
case OP_GETGLOBAL: {
uint8_t cix = RBYTE();
Val varname = ch->consts.d[cix];
CHECK(IS_STRING(varname), "global names must be string");
Val v = ht_get(S, &S->globals, AS_STRING(varname));
PUSH(v);
if (IS_NIL(v)) {
printf("warning: nil global read %s\n", AS_CSTRING(varname));
}
break;
}
case OP_SETGLOBAL: {
uint8_t cix = RBYTE();
Val varname = ch->consts.d[cix];
CHECK(IS_STRING(varname), "global names must be string");
Val v = PEEK();
ht_put(S, &S->globals, AS_STRING(varname), v);
break;
}
case OP_GETLOCAL: {
uint8_t lidx = RBYTE();
PUSH(th->stack[th->fp + lidx]);
break;
}
case OP_SETLOCAL: {
uint8_t lidx = RBYTE();
Val v = PEEK();
th->stack[th->fp + lidx] = v;
break;
}
#define BINARY_OP(opcode, OP, RET_TYPE) \
case opcode: { \
Val b = POP(); \
Val a = POP(); \
CHECK(IS_NUM(a) && IS_NUM(b), "can only do arithmetic on num"); \
PUSH(RET_TYPE(AS_NUM(a) OP AS_NUM(b))); \
} \
break;
#define ARITH_OP(opcode, OP) BINARY_OP(opcode, OP, VAL_NUM)
#define BOOL_OP(opcode, OP) BINARY_OP(opcode, OP, VAL_BOOL)
ARITH_OP(OP_ADD, +)
ARITH_OP(OP_SUB, -)
ARITH_OP(OP_MUL, *)
ARITH_OP(OP_DIV, /)
BOOL_OP(OP_CMP, <)
#undef BINARY_OP
#undef ARITH_OP
#undef BOOL_OP
case OP_EQU: {
Val b = POP();
Val a = POP();
PUSH(VAL_BOOL(val_equal(a,b)));
break;
}
case OP_MOD: {
Val b = POP();
Val a = POP();
CHECK(IS_NUM(a) && IS_NUM(b), "can only do arithmetic on num");
PUSH(VAL_NUM(fmod(AS_NUM(a), AS_NUM(b))));
break;
}
case OP_NIL: PUSH(VAL_NIL); break;
case OP_TRUE: PUSH(VAL_TRUE); break;
case OP_FALSE: PUSH(VAL_FALSE); break;
case OP_TAILCALL:
case OP_CALL: {
// nargs + 1 = function and args
uint8_t len = RBYTE();
uint8_t nargs = len - 1;
Val callee = PEEKN(len);
if (IS_FUNC(callee)) {
ObjFunc *func = AS_FUNC(callee);
CHECK(nargs == func->arity, "func needs exactly %d args, but got %d",func->arity,nargs);
CHECK(th->rsp < MAXDEPTH, "rstack overflow");
if (instr != OP_TAILCALL) {
StackFrame *sf = &th->rstack[th->rsp++];
sf->ip = th->ip;
sf->ch = th->ch;
sf->fp = th->fp;
th->ip = 0;
th->ch = &func->ch;
th->fp = th->sp - len;
} else {
// xxx might invalidate open upvalues
memmove(&th->stack[th->fp], &th->stack[th->sp - len], len*sizeof(Val));
th->sp = th->fp + len;
th->ip = 0;
th->ch = &func->ch;
StackFrame *cur_sf = &th->rstack[th->rsp];
cur_sf->ip = th->ip;
cur_sf->ch = th->ch;
cur_sf->fp = th->fp;
}
} else if (IS_CFUNC(callee)) {
Val *firstarg = &th->stack[th->sp - nargs];
Val res = AS_CFUNC(callee)(S, nargs, firstarg);
th->sp -= len;
PUSH(res);
} else if (IS_ARR(callee)) {
ObjArr *arr = AS_ARR(callee);
CHECK(len == 2, "can only index arr with single argument");
Val vix = PEEK();
CHECK(IS_NUM(vix), "can only index numerically");
size_t ix = (size_t)AS_NUM(vix);
Val res = objarr_get(S, arr, ix);
th->sp -= 2;
PUSH(res);
} else {
ERROR("cannot call %s",typename_str(callee));
}
break;
}
case OP_SETIDX: {
Val vix = POP();
Val varr = POP();
Val v = PEEK();
CHECK(IS_NUM(vix), "can only index numerically");
CHECK(IS_ARR(varr), "can only set index on array");
size_t ix = (size_t)AS_NUM(vix);
ObjArr *arr = AS_ARR(varr);
objarr_put(S, arr, ix, v);
break;
}
case OP_ENDSCOPE: {
uint8_t nlocals = RBYTE();
Val retval = POP();
th->sp -= nlocals;
PUSH(retval);
break;
}
case OP_RET: {
StackFrame *sf = &th->rstack[--th->rsp];
th->ip = sf->ip;
th->ch = sf->ch;
th->fp = sf->fp;
break;
}
case OP_HALT:
return 0;
case OP_ARRNEW: {
ObjArr *a = objarr_new(S);
PUSH(VAL_OBJ(a));
break;
}
case OP_ARRAPPEND: {
Val v = POP();
Val a = PEEK();
CHECK(IS_ARR(a), "can only append to array");
ObjArr *arr = AS_ARR(a);
objarr_append(S, arr, v);
break;
}
case OP_ARRLEN: {
Val v = POP();
CHECK(IS_ARR(v), "can only get length of array");
ObjArr *arr = AS_ARR(v);
PUSH(VAL_NUM(arr->len));
break;
}
default:
ERROR("unknown opcode");
}
}
}
#undef RBYTE
#undef RSHORT
#undef PUSH
#undef POP
#undef PEEK
|