-
Notifications
You must be signed in to change notification settings - Fork 1
/
db_vmint.c
334 lines (319 loc) · 9.45 KB
/
db_vmint.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
/* db_vmint.c - bytecode interpreter for a simple virtual machine
*
* Copyright (c) 2014 by David Michael Betz. All rights reserved.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
#include "db_vm.h"
#include "db_vmdebug.h"
//#define DEBUG
/* interpreter state structure */
typedef struct {
System *sys;
ImageHdr *image;
VMVALUE *stack;
VMVALUE *stackTop;
uint8_t *pc;
VMVALUE *fp;
VMVALUE *sp;
VMVALUE tos;
} Interpreter;
/* stack manipulation macros */
#define Reserve(i, n) do { \
if ((i)->sp - (n) < (i)->stack) \
StackOverflow(i); \
else \
(i)->sp -= (n); \
} while (0)
#define CPush(i, v) do { \
if ((i)->sp <= (i)->stack) \
StackOverflow(i); \
else \
Push(i, v); \
} while (0)
#define Push(i, v) (*--(i)->sp = (v))
#define Pop(i) (*(i)->sp++)
#define Top(i) (*(i)->sp)
#define Drop(i, n) ((i)->sp += (n))
/* prototypes for local functions */
static void DoTrap(Interpreter *i, int op);
static void StackOverflow(Interpreter *i);
#ifdef DEBUG
static void ShowStack(Interpreter *i);
#endif
/* Execute - execute the main code */
int Execute(System *sys, ImageHdr *image, VMVALUE main)
{
size_t stackSize;
Interpreter *i;
VMVALUE tmp;
VMWORD tmpw;
int8_t tmpb;
int cnt;
/* allocate the interpreter state */
if (!(i = (Interpreter *)AllocateFreeSpace(sys, sizeof(Interpreter))))
return VMFALSE;
/* make sure there is space left for the stack */
if ((stackSize = sys->freeTop - sys->freeNext) < MIN_STACK_SIZE)
return VMFALSE;
/* setup the new image */
i->sys = sys;
i->image = image;
i->stack = (VMVALUE *)((uint8_t *)i + sizeof(Interpreter));
i->stackTop = (VMVALUE *)((uint8_t *)i->stack + stackSize);
/* initialize */
i->pc = (uint8_t *)main;
i->sp = i->fp = i->stackTop;
if (setjmp(i->sys->errorTarget))
return VMFALSE;
for (;;) {
#ifdef DEBUG
ShowStack(i);
DecodeInstruction(i->pc, i->pc);
#endif
switch (VMCODEBYTE(i->pc++)) {
case OP_HALT:
return VMTRUE;
case OP_BRT:
for (tmpw = 0, cnt = sizeof(VMWORD); --cnt >= 0; )
tmpw = (tmpw << 8) | VMCODEBYTE(i->pc++);
if (i->tos)
i->pc += tmpw;
i->tos = Pop(i);
break;
case OP_BRTSC:
for (tmpw = 0, cnt = sizeof(VMWORD); --cnt >= 0; )
tmpw = (tmpw << 8) | VMCODEBYTE(i->pc++);
if (i->tos)
i->pc += tmpw;
else
i->tos = Pop(i);
break;
case OP_BRF:
for (tmpw = 0, cnt = sizeof(VMWORD); --cnt >= 0; )
tmpw = (tmpw << 8) | VMCODEBYTE(i->pc++);
if (!i->tos)
i->pc += tmpw;
i->tos = Pop(i);
break;
case OP_BRFSC:
for (tmpw = 0, cnt = sizeof(VMWORD); --cnt >= 0; )
tmpw = (tmpw << 8) | VMCODEBYTE(i->pc++);
if (!i->tos)
i->pc += tmpw;
else
i->tos = Pop(i);
break;
case OP_BR:
for (tmpw = 0, cnt = sizeof(VMWORD); --cnt >= 0; )
tmpw = (tmpw << 8) | VMCODEBYTE(i->pc++);
i->pc += tmpw;
break;
case OP_NOT:
i->tos = (i->tos ? VMFALSE : VMTRUE);
break;
case OP_NEG:
i->tos = -i->tos;
break;
case OP_ADD:
tmp = Pop(i);
i->tos = tmp + i->tos;
break;
case OP_SUB:
tmp = Pop(i);
i->tos = tmp - i->tos;
break;
case OP_MUL:
tmp = Pop(i);
i->tos = tmp * i->tos;
break;
case OP_DIV:
tmp = Pop(i);
i->tos = (i->tos == 0 ? 0 : tmp / i->tos);
break;
case OP_REM:
tmp = Pop(i);
i->tos = (i->tos == 0 ? 0 : tmp % i->tos);
break;
case OP_BNOT:
i->tos = ~i->tos;
break;
case OP_BAND:
tmp = Pop(i);
i->tos = tmp & i->tos;
break;
case OP_BOR:
tmp = Pop(i);
i->tos = tmp | i->tos;
break;
case OP_BXOR:
tmp = Pop(i);
i->tos = tmp ^ i->tos;
break;
case OP_SHL:
tmp = Pop(i);
i->tos = tmp << i->tos;
break;
case OP_SHR:
tmp = Pop(i);
i->tos = tmp >> i->tos;
break;
case OP_LT:
tmp = Pop(i);
i->tos = (tmp < i->tos ? VMTRUE : VMFALSE);
break;
case OP_LE:
tmp = Pop(i);
i->tos = (tmp <= i->tos ? VMTRUE : VMFALSE);
break;
case OP_EQ:
tmp = Pop(i);
i->tos = (tmp == i->tos ? VMTRUE : VMFALSE);
break;
case OP_NE:
tmp = Pop(i);
i->tos = (tmp != i->tos ? VMTRUE : VMFALSE);
break;
case OP_GE:
tmp = Pop(i);
i->tos = (tmp >= i->tos ? VMTRUE : VMFALSE);
break;
case OP_GT:
tmp = Pop(i);
i->tos = (tmp > i->tos ? VMTRUE : VMFALSE);
break;
case OP_LIT:
for (tmp = 0, cnt = sizeof(VMVALUE); --cnt >= 0; )
tmp = (tmp << 8) | VMCODEBYTE(i->pc++);
CPush(i, i->tos);
i->tos = tmp;
break;
case OP_SLIT:
tmpb = (int8_t)VMCODEBYTE(i->pc++);
CPush(i, i->tos);
i->tos = tmpb;
break;
case OP_LOAD:
i->tos = *(VMVALUE *)i->tos;
break;
case OP_LOADB:
i->tos = *(uint8_t *)i->tos;
break;
case OP_STORE:
tmp = Pop(i);
*(VMVALUE *)tmp = i->tos;
break;
case OP_STOREB:
tmp = Pop(i);
*(uint8_t *)tmp = i->tos;
break;
case OP_LADDR:
tmpb = (int8_t)VMCODEBYTE(i->pc++);
CPush(i, i->tos);
i->tos = (VMVALUE)&i->fp[(int)tmpb];
break;
case OP_INDEX:
tmp = Pop(i);
i->tos = tmp + i->tos * sizeof (VMVALUE);
break;
case OP_CALL:
++i->pc; // skip over the argument count
tmp = i->tos;
i->tos = (VMVALUE)i->pc;
i->pc = (uint8_t *)tmp;
break;
case OP_FRAME:
cnt = VMCODEBYTE(i->pc++);
tmp = (VMVALUE)i->fp;
i->fp = i->sp;
Reserve(i, cnt);
i->sp[0] = i->tos;
i->sp[1] = tmp;
break;
case OP_RETURN:
i->pc = (uint8_t *)Top(i);
i->sp = i->fp;
Drop(i, i->pc[-1]);
i->fp = (VMVALUE *)i->fp[-1];
break;
case OP_DROP:
i->tos = Pop(i);
break;
case OP_DUP:
CPush(i, i->tos);
break;
case OP_TUCK:
CPush(i, i->tos);
tmp = i->sp[0];
i->sp[0] = i->sp[1];
i->sp[1] = tmp;
break;
case OP_NATIVE:
for (tmp = 0, cnt = sizeof(VMUVALUE); --cnt >= 0; )
tmp = (tmp << 8) | VMCODEBYTE(i->pc++);
break;
case OP_TRAP:
DoTrap(i, VMCODEBYTE(i->pc++));
break;
default:
Abort(i->sys, "undefined opcode 0x%02x", VMCODEBYTE(i->pc - 1));
break;
}
}
}
static void DoTrap(Interpreter *i, int op)
{
switch (op) {
case TRAP_GetChar:
Push(i, i->tos);
i->tos = VM_getchar();
break;
case TRAP_PutChar:
VM_putchar(i->tos);
i->tos = Pop(i);
break;
case TRAP_PrintStr:
VM_printf("%s", (char *)i->tos);
i->tos = *i->sp++;
break;
case TRAP_PrintInt:
VM_printf("%d", i->tos);
i->tos = *i->sp++;
break;
case TRAP_PrintTab:
VM_putchar('\t');
break;
case TRAP_PrintNL:
VM_putchar('\n');
break;
case TRAP_PrintFlush:
VM_flush();
break;
default:
Abort(i->sys, "undefined trap %d", op);
break;
}
}
static void StackOverflow(Interpreter *i)
{
Abort(i->sys, "stack overflow");
}
#ifdef DEBUG
static void ShowStack(Interpreter *i)
{
VMVALUE *p;
if (i->sp < i->stackTop) {
VM_printf(" %d", i->tos);
for (p = i->sp; p < i->stackTop - 1; ++p) {
if (p == i->fp)
VM_printf(" <fp>");
VM_printf(" %d", *p);
}
VM_printf("\n");
}
}
#endif