-
Notifications
You must be signed in to change notification settings - Fork 2
/
svm.c
278 lines (271 loc) · 6.73 KB
/
svm.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
/*
* SVM: A Small Virtual Machine using Giant Switch
* by Takuo Watanabe
*/
#include <stdio.h>
#include <stdlib.h>
#include "vm.h"
#include "fun.h"
void vm (Bcode code[], size_t codelen,
Value *vs_base, size_t vs_size,
Value gv[], size_t gv_size,
vm_mode_t mode) {
Bcode *ip = code; // 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
for (;;) {
#ifndef DISABLE_EXECUTION_TRACE
nsteps++;
dump_vm(nsteps, ip, code, lv, fp, sp, vs_base, gv, gv_size, mode);
#endif
switch (*ip) {
case i_nop:
ip++;
break;
case i_dup:
sp--;
sp[0] = sp[1];
ip++;
break;
case i_pop:
sp++;
ip++;
break;
case i_goto:
ip += B2INT(ip[1], ip[2]) + 3;
break;
case i_ifeq:
if ((int)sp[0] == 0)
ip += B2INT(ip[1], ip[2]);
ip += 3;
sp++;
break;
case i_ifne:
if ((int)sp[0] != 0)
ip += B2INT(ip[1], ip[2]);
ip += 3;
sp++;
break;
case i_iflt:
if ((int)sp[0] < 0)
ip += B2INT(ip[1], ip[2]);
ip += 3;
sp++;
break;
case i_ifle:
if ((int)sp[0] <= 0)
ip += B2INT(ip[1], ip[2]);
ip += 3;
sp++;
break;
case i_ifgt:
if ((int)sp[0] > 0)
ip += B2INT(ip[1], ip[2]);
ip += 3;
sp++;
break;
case i_ifge:
if ((int)sp[0] >= 0)
ip += B2INT(ip[1], ip[2]);
ip += 3;
sp++;
break;
case i_giload:
case i_gaload:
sp--;
/* sp[0] = gv[(int)ip[1] << 8 | (int)ip[2]]; */
sp[0] = gv[B2INT(ip[1], ip[2])];
ip += 3;
break;
case i_gistore:
case i_gastore:
/* gv[(int)ip[1] << 8 | (int)ip[2]] = sp[0]; */
gv[B2INT(ip[1], ip[2])] = sp[0];
sp++;
ip += 3;
break;
case i_iload:
case i_aload:
sp--;
/* sp[0] = lv[-(int)ip[1]]; */
sp[0] = lv[-B1INT(ip[1])];
ip += 2;
break;
case i_iload_0:
case i_aload_0:
sp--;
sp[0] = lv[0];
ip += 1;
break;
case i_iload_1:
case i_aload_1:
sp--;
sp[0] = lv[-1];
ip += 1;
break;
case i_iload_2:
case i_aload_2:
sp--;
sp[0] = lv[-2];
ip += 1;
break;
case i_iload_3:
case i_aload_3:
sp--;
sp[0] = lv[-3];
ip += 1;
break;
case i_istore:
case i_astore:
/* lv[-(int)ip[1]] = sp[0]; */
lv[-B1INT(ip[1])] = sp[0];
sp++;
ip += 2;
break;
case i_istore_0:
case i_astore_0:
lv[0] = sp[0];
sp++;
ip += 1;
break;
case i_istore_1:
case i_astore_1:
lv[-1] = sp[0];
sp++;
ip += 1;
break;
case i_istore_2:
case i_astore_2:
lv[-2] = sp[0];
sp++;
ip += 1;
break;
case i_istore_3:
case i_astore_3:
lv[-3] = sp[0];
sp++;
ip += 1;
break;
case i_bipush:
sp--;
/* sp[0] = (Value)ip[1]; */
sp[0] = (Value)B1INT(ip[1]);
ip += 2;
break;
case i_sipush:
sp--;
sp[0] = (Value)((int)ip[1] + (int)ip[2] * 256);
ip += 3;
break;
case i_iconst_m1:
sp--;
sp[0] = (Value)-1;
ip++;
break;
case i_iconst_0:
sp--;
sp[0] = (Value)0;
ip++;
break;
case i_iconst_1:
sp--;
sp[0] = (Value)1;
ip++;
break;
case i_iconst_2:
sp--;
sp[0] = (Value)2;
ip++;
break;
case i_iadd:
sp[1] = sp[1] + sp[0];
sp++;
ip++;
break;
case i_isub:
sp[1] = sp[1] - sp[0];
sp++;
ip++;
break;
case i_imul:
sp[1] = sp[1] * sp[0];
sp++;
ip++;
break;
case i_idiv:
sp[1] = sp[1] / sp[0];
sp++;
ip++;
break;
case i_irem:
sp[1] = sp[1] % sp[0];
sp++;
ip++;
break;
case i_icall:
case i_acall: {
Function f = (Function)sp[0];
int nargs = (int)ip[1];
Value *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 = f->code;
break;
}
case i_ticall:
case i_tacall: {
Function f = (Function)sp[0];
int nargs = (int)ip[1];
Value ip_save = fp[0];
Value lv_save = fp[1];
Value fp_save = fp[2];
Value *arg = sp + nargs;
for (int i = 0; i < nargs ; i++)
lv[-i] = arg[-i];
fp = lv - nargs - f->nlv - 3;
sp = fp;
fp[0] = ip_save;
fp[1] = lv_save;
fp[2] = fp_save;
ip = f->code;
break;
}
case i_sicall:
case i_sacall: {
Function f = (Function)sp[0];
int nargs = (int)ip[1];
Value *arg = sp + nargs;
for (int i = 0; i < nargs; i++)
lv[-i] = arg[-i];
sp = fp;
ip = f->code;
break;
}
case i_iret:
case i_aret:
lv[0] = sp[0];
sp = lv;
ip = (Bcode *)fp[0];
lv = (Value *)fp[1];
fp = (Value *)fp[2];
break;
case i_halt:
return;
case i_showtop:
printf(">>> %d\n", (int)sp[0]);
sp++;
ip++;
break;
default:
fprintf(stderr, "Invalid instruction: %d\n", (int)ip[-1]);
return;
}
}
}