-
Notifications
You must be signed in to change notification settings - Fork 0
/
vm.c
472 lines (427 loc) · 9.88 KB
/
vm.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
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/*
*
* vm.c
* (Virtual Machine)
* Instruction execution - the heart of an interpreter.
*
*/
/* SMPL headers */
#include "state.h"
#include "parser.h"
#include "opcodes.h"
#include "smpl.h"
#include "limits.h"
#include "mem.h"
#include "debug.h"
#include "api.h"
static sp_Label *getlbl (sp_State *S, uint lbl)
{
for(uint j = 0; j < S->lbltop; ++j)
{
if(S->lbls[j].id == lbl)
{
return &S->lbls[j];
}
}
return NULL;
}
#define insat(S,i) ((S)->inss[(i)]) /* get the instruction at index 'i' */
#define ins(S) insat((S), (*pc)++) /* get the current instruction and advance the program counter */
#define argat(S,i) ((S)->opargs[(i)]) /* get the instruction argument at index 'i' */
#define getarg(S) argat((S), GET_ARG(i)) /* get an argument attached to the current instruction */
//#define vmdispatch(op) switch((op))
#define vmcase(op) case (op):
#define vmbreak break
#define vmnext continue
#define savepc(S,ofs) (sp_push_number((S), *pc + (ofs))) /* save the current program counter */
#define restorepc(S) ((*pc = obj_num(sp_top((S), 0))), (sp_pop((S), 1))) /* restore the last program counter */
#define VMSTATE_ARGS sp_State *S, uint *pc, sp_Instruction end, sp_ObjectType m, sp_Instruction i
#define VMSTATE S, pc, end, m, i
static sp_Dim *get_dim (VMSTATE_ARGS)
{
sp_Object *arg = getarg(S);
sp_String *id = obj_str(arg);
sp_Object *o = sp_Sget_odglobal(S, id);
sp_Dim *dim = obj_dim(o);
return dim;
}
static uint32_t get_dim_index (VMSTATE_ARGS, sp_Dim *dim)
{
uint32_t ai = 0; /* absolute index */
uint32_t offset = 1;
uint32_t dims = ins(S);
// 2, 2, 2
// 1, 1, 1
// 1, 1, 2
// 1, 2, 1
// 1, 2, 2
// 2, 1, 1
// 2, 1, 2
// 2, 2, 1
// 2, 2, 2
// ai = 7
/*
ai += 1
ai += 2
ai += 4
*/
for(uint32_t i = 0; i < dims; i++)
{
uint32_t num = sp_checknum(S);
ai += (num - 1) * offset;
offset *= dim->lens[i];
}
return ai;
}
/*
** Evaluate next instructions until the instruction is equal to 'end'
*/
static void exec (sp_State *S, uint *pc, sp_Instruction end, sp_ObjectType m)
{
sp_Instruction i;
while(*pc < S->instop && (i = ins(S)) != end)
{
sp_OpCode opc = GET_OPCODE(i);
spVDO_printinc(S, *pc - 1, "> %s ", spD_op2str(opc));
#define vmdispatch(op) switch((op))
vmdispatch(opc)
{
vmcase(OP_DUP)
{
sp_push(S, spO_new_copy(S, sp_top(S, 0)));
vmbreak;
}
vmcase(OP_POP)
{
sp_pop(S, 1);
vmbreak;
}
vmcase(OP_PRINTNL)
{
sp_io_printstr(S, "\n");
vmbreak;
}
vmcase(OP_INPUT)
{
sp_io_getstr(S);
sp_push_String(S, S->s);
vmbreak;
}
vmcase(OP_PRINT)
{
sp_print(S);
vmbreak;
}
vmcase(OP_LOADS)
{
sp_Object *arg = getarg(S);
sp_String *s = obj_str(arg);
spDO_printf("\"%s\"", s->src);
sp_push_String(S, s);
vmbreak;
}
vmcase(OP_LOADN)
{
sp_Object *arg = getarg(S);
sp_Number n = obj_num(arg);
spDO_printf("%lf", n);
sp_push_number(S, n);
vmbreak;
}
vmcase(OP_SETVAR)
{
sp_Object *arg = getarg(S);
sp_String *id = obj_str(arg);
spDO_printf("\"%s\"", id->src);
sp_Sset_global(S, id);
vmbreak;
}
vmcase(OP_DIM)
{
uint32_t argi = GET_ARG(i);
sp_Object *arg = getarg(S);
sp_Object *o = argat(S, argi + 1);
sp_Dim *dim = obj_dim(o);
uint32_t dims = ins(S);
uint32_t len = 1; /* intial length - to be determined */
uint32_t *lens = malloc(dims * sizeof(*lens)); /* length of individual "dimensions" */
for(uint32_t di = 0; di < dims; di++)
{
uint32_t num = sp_checknum(S);
len *= num;
lens[di] = num;
}
sp_Object **v = calloc(len, sizeof(*v)); /* calloc values */
obj_setdim(o, spA_new_set(S, v, len)); /* allocate new DIM and assign it's value to object */
obj_settype(o, OT_DIM);
dim = obj_dim(o);
dim->lens = lens; /* TODO: put into "spA_new_set" */
vmbreak;
}
vmcase(OP_GETNAME)
{
sp_Object *arg = getarg(S);
sp_String *id = obj_str(arg);
spDO_printf("\"%s\"", id->src);
sp_Smget_global(S, m, id);
#ifdef SMPL_DEBUG
/*
* TODO: Remaster
*/
spDO_printf(" (");
sp_push(S, spO_new_copy(S, sp_top(S, 0)));
sp_print(S);
spDO_printf(")");
#endif
vmbreak;
}
vmcase(OP_SETDIM)
{
sp_Dim *dim = get_dim(VMSTATE);
uint32_t ai = get_dim_index(VMSTATE, dim);
// printf("1\n");
sp_Object *obj = dim->src[ai];
// printf("2\n");
if(obj == NULL)
{
dim->src[ai] = spO_new(S);
obj = dim->src[ai];
}
// printf("3\n");
// printf(">%p\n", obj);
spO_copy(S, obj, sp_top(S, 0));
// printf("4\n");
sp_pop(S, 1);
vmbreak;
}
vmcase(OP_GETDIM)
{
sp_Dim *dim = get_dim(VMSTATE);
uint32_t ai = get_dim_index(VMSTATE, dim);
sp_Object *obj = dim->src[ai];
if(obj != NULL)
sp_push(S, spO_new_copy(S, obj));
vmbreak;
}
vmcase(OP_SAVE)
{
savepc(S, 3);
vmbreak;
}
vmcase(OP_RESTORE)
{
restorepc(S);
vmbreak;
}
vmcase(OP_FN)
{
sp_Object *arg = getarg(S);
sp_String *fid = obj_str(arg);
sp_Object *f = sp_Sget_ofglobal(S, fid);
sp_ObjectType objt = OT_NUM | OT_STR;
sp_Object *ov; /* old value */
sp_Object *v = sp_Smget_oglobal(S, objt, obj_fsarg(f)); /* variable that argument possibly replaces */
/* argument replaces an variable, save it */
if(v != NULL)
{
ov = spO_new_copy(S, v);
}
sp_Smset_oglobal(S, objt, obj_fsarg(f), sp_top(S, 0));
sp_pop(S, 1);
spDO_printf("\"%s\"\n", fid->src);
uint opc = *pc; /* save current pc */
*pc = obj_fi(f); /* jump to function's source */
exec(S, pc, OP_FNEND, objt); /* execute to the end of function */
*pc = opc; /* restore the saved pc */
/* restore the old variable */
if(v != NULL)
{
sp_Smset_oglobal(S, objt, obj_fsarg(f), ov);
spO_free(ov);
}
spDO_dectabs(S);
vmnext;
}
vmcase(OP_DEF)
{
uint argi = GET_ARG(i);
uint fsrc = *pc + 2; /* function src - skip the jump to the end */
sp_String *fid = obj_str(argat(S,argi)); /* function id (first oparg) */
sp_Object *farg = argat(S, argi + 1); /* function arg (second oparg) */
spDO_printf("\"%s\"", fid->src);
sp_Sset_ofglobal(S, fid, fsrc, farg);
vmbreak;
}
vmcase(OP_CALL)
{
sp_Object *o = getarg(S);
spDO_printf("\"%s\"", obj_str(o)->src);
sp_Sget_cfglobal(S, obj_str(o));
sp_call(S, ins(S));
vmbreak;
}
vmcase(OP_JUMP)
{
uint npc = ins(S);
spDO_printf("%u", npc);
*pc = npc;
vmbreak;
}
vmcase(OP_JLBL)
{
uint ln = ins(S);
uint lblid = ins(S);
// printf("label id: %d\n", lblid);
sp_Label *lbl = getlbl(S, lblid);
*pc = lbl->i;
spDO_printf("%u (LBL %u)", *pc, lblid);
vmbreak;
}
vmcase(OP_JTRUE)
{
uint npc = S->inss[*pc];
spDO_printf("%u", npc);
if(sp_checknum(S))
*pc = npc;
else ++*pc;
vmbreak;
}
vmcase(OP_JFALSE)
{
uint npc = S->inss[*pc];
spDO_printf("%u", npc);
if(!sp_checknum(S))
*pc = npc;
else ++*pc;
vmbreak;
}
vmcase(OP_NEG)
{
spDO_printf("-");
sp_unary_arith(S, OPR_NEG);
vmbreak;
}
vmcase(OP_NOT)
{
spDO_printf("!");
sp_unary_arith(S, OPR_NOT);
vmbreak;
}
vmcase(OP_OR)
{
spDO_printf("||");
sp_binary_arith(S, OPR_OR);
vmbreak;
}
vmcase(OP_AND)
{
spDO_printf("&&");
sp_binary_arith(S, OPR_AND);
vmbreak;
}
vmcase(OP_LT)
{
spDO_printf("<");
sp_binary_arith(S, OPR_LT);
vmbreak;
}
vmcase(OP_LE)
{
spDO_printf("<=");
sp_binary_arith(S, OPR_LE);
vmbreak;
}
vmcase(OP_GT)
{
spDO_printf(">");
sp_binary_arith(S, OPR_GT);
vmbreak;
}
vmcase(OP_GE)
{
spDO_printf(">=");
sp_binary_arith(S, OPR_GE);
vmbreak;
}
vmcase(OP_NE)
{
spDO_printf("!=");
sp_binary_arith(S, OPR_NE);
vmbreak;
}
vmcase(OP_EQ)
{
spDO_printf("==");
sp_binary_arith(S, OPR_EQ);
vmbreak;
}
vmcase(OP_PLUS)
{
spDO_printf("+");
sp_binary_arith(S, OPR_PLUS);
vmbreak;
}
vmcase(OP_MINUS)
{
spDO_printf("-");
sp_binary_arith(S, OPR_MINUS);
vmbreak;
}
vmcase(OP_MUL)
{
spDO_printf("*");
sp_binary_arith(S, OPR_MUL);
vmbreak;
}
vmcase(OP_DIV)
{
spDO_printf("/");
sp_binary_arith(S, OPR_DIV);
vmbreak;
}
vmcase(OP_MOD)
{
spDO_printf("%");
sp_binary_arith(S, OPR_MOD);
vmbreak;
}
}
spDO_dectabs(S);
spDO_puts("");
}
}
/*
** Main interpreter loop
*/
void spV_execute (sp_State *S)
{
spDO_printf("========================================================================================\n"
"| EXECUTION\n"
"========================================================================================\n");
#ifdef SMPL_DEBUG
if(S->instop)
{
puts("[PC]-----------------------------------------------");
for(int i = 0; i < S->instop; ++i)
{
sp_OpCode op = GET_OPCODE(S->inss[i]);
printf("[%2u] %-7s [%2u]\n", i, spD_op2str(op), op);
switch(op)
{
case OP_JLBL:
spDO_incprint(S, ++i, "> line number\n");
spDO_printdec(S, ++i, "> label\n");
continue;
case OP_JTRUE:
case OP_JFALSE:
spDO_incprint(S, ++i, "> condition\n");
spDO_dectabs(S);
continue;
}
}
puts("\n[PC]-----------------------------------------------");
}
#endif
uint pc = 0;
exec(S, &pc, OP_END, OT_ANY); /* execute the whole program */
}