-
Notifications
You must be signed in to change notification settings - Fork 2
/
tvm.c
390 lines (378 loc) · 6.96 KB
/
tvm.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
/*
* A Small Virtual Machine using Direct Threaded Code (for GCC)
* by Takuo Watanabe
*/
#include <stdio.h>
#include <stdlib.h>
#include "vm.h"
#include "fun.h"
#ifdef DISABLE_EXECUTION_TRACE
#define NEXT goto **ip
#else
#define NEXT dump_vm(++nsteps, code + (ip - tcode), code, \
lv, fp, sp, vs_base, gv, gv_size, mode); \
goto **ip
#endif
typedef void *Tcode;
static void b2t (Bcode code[], Tcode tcode[], size_t codelen,
const Tcode labels[]) {
int i = 0;
while (i < codelen) {
tcode[i] = labels[code[i]];
switch (code[i]) {
case i_goto:
case i_ifeq:
case i_ifne:
case i_iflt:
case i_ifle:
case i_ifgt:
case i_ifge:
tcode[i + 1] = (Tcode)B2INT(code[i + 1], code[i + 2]);
tcode[i + 2] = NULL;
i += 2;
break;
case i_giload:
case i_gaload:
case i_gistore:
case i_gastore:
/* tcode[i + 1] = (Tcode)((int)code[i+1] << 8 | (int)code[i+2]); */
tcode[i + 1] = (Tcode)B2INT(code[i + 1], code[i + 2]);
tcode[i + 2] = NULL;
i += 2;
break;
case i_iload:
case i_aload:
case i_istore:
case i_astore:
case i_bipush:
case i_icall:
case i_acall:
case i_ticall:
case i_tacall:
case i_sicall:
case i_sacall:
/* tcode[i+1] = (Tcode)((int)code[i+1]); */
tcode[i + 1] = (Tcode)B1INT(code[i + 1]);
i++;
break;
case i_sipush:
tcode[i + 1] = (Tcode)((long)code[1] + (long)code[2] * 256);
tcode[i + 2] = NULL;
i += 2;
break;
}
i++;
}
}
void vm (Bcode code[], size_t codelen,
Value *vs_base, size_t vs_size,
Value gv[], size_t gv_size,
vm_mode_t mode) {
static const Tcode labels[] = {
&&l_nop,
&&l_dup,
&&l_pop,
&&l_goto,
&&l_ifeq,
&&l_ifne,
&&l_iflt,
&&l_ifle,
&&l_ifgt,
&&l_ifge,
&&l_giload,
&&l_gaload,
&&l_gistore,
&&l_gastore,
&&l_iload,
&&l_aload,
&&l_iload_0,
&&l_aload_0,
&&l_iload_1,
&&l_aload_1,
&&l_iload_2,
&&l_aload_2,
&&l_iload_3,
&&l_aload_3,
&&l_istore,
&&l_astore,
&&l_istore_0,
&&l_astore_0,
&&l_istore_1,
&&l_astore_1,
&&l_istore_2,
&&l_astore_2,
&&l_istore_3,
&&l_astore_3,
&&l_bipush,
&&l_sipush,
&&l_iconst_m1,
&&l_iconst_0,
&&l_iconst_1,
&&l_iconst_2,
&&l_iadd,
&&l_isub,
&&l_imul,
&&l_idiv,
&&l_irem,
&&l_icall,
&&l_acall,
&&l_ticall,
&&l_tacall,
&&l_sicall,
&&l_sacall,
&&l_iret,
&&l_aret,
&&l_halt,
&&l_showtop
};
Tcode tcode[codelen];
/* machine registers */
Tcode *ip; /* instruction pointer */
Value *sp = vs_base; /* stack pointer */
Value *fp = sp; /* frame pointer */
Value *lv = sp; /* local variable base pointer */
#ifndef DISABLE_EXECUTION_TRACE
long nsteps = 0;
#endif
Function f;
int nargs;
Value ip_save, lv_save, fp_save;
Value *arg;
Value *newfp;
b2t(code, tcode, codelen, labels);
/* start VM */
ip = tcode;
NEXT;
return;
l_nop:
ip++;
NEXT;
l_dup:
sp--;
sp[0] = sp[1];
ip++;
NEXT;
l_pop:
sp++;
ip++;
NEXT;
l_goto:
ip += (long)ip[1] + 3;
NEXT;
l_ifeq:
if ((int)sp[0] == 0)
ip += (long)ip[1];
ip += 3;
sp++;
NEXT;
l_ifne:
if ((int)sp[0] != 0)
ip += (long)ip[1];
ip += 3;
sp++;
NEXT;
l_iflt:
if ((int)sp[0] < 0)
ip += (long)ip[1];
ip += 3;
sp++;
NEXT;
l_ifle:
if ((int)sp[0] <= 0)
ip += (long)ip[1];
ip += 3;
sp++;
NEXT;
l_ifgt:
if ((int)sp[0] > 0)
ip += (long)ip[1];
ip += 3;
sp++;
NEXT;
l_ifge:
if ((int)sp[0] >= 0)
ip += (long)ip[1];
ip += 3;
sp++;
NEXT;
l_giload:
l_gaload:
sp--;
sp[0] = gv[(long)ip[1]];
ip += 3;
NEXT;
l_gistore:
l_gastore:
gv[(long)ip[1]] = sp[0];
sp++;
ip += 3;
NEXT;
l_iload:
l_aload:
sp--;
sp[0] = lv[-(long)ip[1]];
ip += 2;
NEXT;
l_iload_0:
l_aload_0:
sp--;
sp[0] = lv[0];
ip += 1;
NEXT;
l_iload_1:
l_aload_1:
sp--;
sp[0] = lv[-1];
ip += 1;
NEXT;
l_iload_2:
l_aload_2:
sp--;
sp[0] = lv[-2];
ip += 1;
NEXT;
l_iload_3:
l_aload_3:
sp--;
sp[0] = lv[-3];
ip += 1;
NEXT;
l_istore:
l_astore:
lv[-(long)ip[1]] = sp[0];
sp++;
ip += 2;
NEXT;
l_istore_0:
l_astore_0:
lv[0] = sp[0];
sp++;
ip += 1;
NEXT;
l_istore_1:
l_astore_1:
lv[-1] = sp[0];
sp++;
ip += 1;
NEXT;
l_istore_2:
l_astore_2:
lv[-2] = sp[0];
sp++;
ip += 1;
NEXT;
l_istore_3:
l_astore_3:
lv[-3] = sp[0];
sp++;
ip += 1;
NEXT;
l_bipush:
sp--;
sp[0] = (Value)ip[1];
ip += 2;
NEXT;
l_sipush:
sp--;
sp[0] = (Value)ip[1];
ip += 3;
NEXT;
l_iconst_m1:
sp--;
sp[0] = (Value)-1;
ip += 1;
NEXT;
l_iconst_0:
sp--;
sp[0] = (Value)0;
ip += 1;
NEXT;
l_iconst_1:
sp--;
sp[0] = (Value)1;
ip += 1;
NEXT;
l_iconst_2:
sp--;
sp[0] = (Value)2;
ip += 1;
NEXT;
l_iadd:
sp[1] = sp[1] + sp[0];
sp++;
ip++;
NEXT;
l_isub:
sp[1] = sp[1] - sp[0];
sp++;
ip++;
NEXT;
l_imul:
sp[1] = sp[1] * sp[0];
sp++;
ip++;
NEXT;
l_idiv:
sp[1] = sp[1] / sp[0];
sp++;
ip++;
NEXT;
l_irem:
sp[1] = sp[1] % sp[0];
sp++;
ip++;
NEXT;
l_icall:
l_acall:
f = (Function)sp[0];
nargs = (long)ip[1];
newfp = sp - f->nlv - 2;
newfp[0] = (Value)(ip + 2);
newfp[1] = (Value)lv;
newfp[2] = (Value)fp;
lv = sp + nargs;
sp = fp = newfp;
ip = tcode + (f->code - code);
NEXT;
l_ticall:
l_tacall:
f = (Function)sp[0];
nargs = (long)ip[1];
ip_save = fp[0];
lv_save = fp[1];
fp_save = fp[2];
arg = sp + nargs;
for (int i = 0; i < nargs ; i++)
lv[-i] = arg[-i];
sp = fp = lv - nargs - f->nlv - 3;
fp[0] = ip_save;
fp[1] = lv_save;
fp[2] = fp_save;
ip = tcode + (f->code - code);
NEXT;
l_sicall:
l_sacall:
f = (Function)sp[0];
nargs = (long)ip[1];
arg = sp + nargs;
for (int i = 0; i < nargs; i++)
lv[-i] = arg[-i];
sp = fp;
ip = tcode + (f->code - code);
NEXT;
l_iret:
l_aret:
lv[0] = sp[0];
sp = lv;
ip = (Tcode)fp[0];
lv = (Value *)fp[1];
fp = (Value *)fp[2];
NEXT;
l_halt:
return;
l_showtop:
printf(">>> %d\n", (int)sp[0]);
sp++;
ip++;
NEXT;
}