-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvm.c
More file actions
389 lines (353 loc) · 9.37 KB
/
Copy pathvm.c
File metadata and controls
389 lines (353 loc) · 9.37 KB
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
/*
* VM - Virtual Machine / Interpreter for PL/0
*
* A simple stack-based virtual machine that executes bytecode generated
* by the compiler. Uses a P-code style instruction set similar to the
* original PL/0 implementation by Wirth.
*
* Stack Layout:
* [0] = Return address
* [1] = Static link (for accessing outer scopes)
* [2] = First local variable
* ...
* [sp] = Top of stack (for computations)
*
* All operations work on the stack - this is a "0-address" machine
* where arithmetic instructions implicitly pop operands and push results.
*/
#include "pl0.h"
/*
* Initialize VM with bytecode to execute.
*/
void vm_init(VM *vm, Instruction *code, int code_size)
{
vm->code = code;
vm->code_size = code_size;
vm->sp = -1; /* Stack is empty */
vm->pc = 0; /* Start at first instruction */
vm->trace = 0;
}
/*
* Enable or disable instruction tracing.
*/
void vm_set_trace(VM *vm, int trace)
{
vm->trace = trace;
}
/*
* Print current instruction for debugging.
*/
static void vm_trace(VM *vm)
{
Instruction *instr = &vm->code[vm->pc];
const char *opname;
switch (instr->op) {
case OP_CONST:
opname = "CONST";
break;
case OP_LOAD:
opname = "LOAD";
break;
case OP_STORE:
opname = "STORE";
break;
case OP_ADD:
opname = "ADD";
break;
case OP_SUB:
opname = "SUB";
break;
case OP_MUL:
opname = "MUL";
break;
case OP_DIV:
opname = "DIV";
break;
case OP_EQ:
opname = "EQ";
break;
case OP_NEQ:
opname = "NEQ";
break;
case OP_LT:
opname = "LT";
break;
case OP_LTE:
opname = "LTE";
break;
case OP_GT:
opname = "GT";
break;
case OP_GTE:
opname = "GTE";
break;
case OP_ODD:
opname = "ODD";
break;
case OP_READ:
opname = "READ";
break;
case OP_WRITE:
opname = "WRITE";
break;
case OP_JMP:
opname = "JMP";
break;
case OP_JZ:
opname = "JZ";
break;
case OP_CALL:
opname = "CALL";
break;
case OP_RET:
opname = "RET";
break;
case OP_HALT:
opname = "HALT";
break;
default:
opname = "???";
break;
}
printf("PC=%04d SP=%04d: %s", vm->pc, vm->sp, opname);
if (instr->op == OP_CONST || instr->op == OP_LOAD || instr->op == OP_STORE ||
instr->op == OP_JMP || instr->op == OP_JZ || instr->op == OP_CALL) {
printf(" %d", instr->value);
}
printf("\n");
}
/*
* Execute the bytecode. Returns 0 on success, non-zero on error.
*/
int vm_run(VM *vm)
{
while (vm->pc >= 0 && vm->pc < vm->code_size) {
Instruction *instr = &vm->code[vm->pc];
if (vm->trace) {
vm_trace(vm);
}
vm->pc++;
switch (instr->op) {
case OP_CONST:
if (vm->sp >= MAX_STACK_SIZE - 1) {
fprintf(stderr, "Error: stack overflow\n");
return -1;
}
vm->stack[++vm->sp] = instr->value;
break;
case OP_LOAD: {
int addr = instr->value;
if (vm->sp >= MAX_STACK_SIZE - 1) {
fprintf(stderr, "Error: stack overflow\n");
return -1;
}
vm->stack[++vm->sp] = vm->stack[addr];
break;
}
case OP_STORE: {
int addr = instr->value;
if (vm->sp < 0) {
fprintf(stderr, "Error: stack underflow\n");
return -1;
}
vm->stack[addr] = vm->stack[vm->sp--];
break;
}
case OP_ADD: {
if (vm->sp < 1) {
fprintf(stderr, "Error: stack underflow\n");
return -1;
}
int b = vm->stack[vm->sp--];
int a = vm->stack[vm->sp--];
vm->stack[++vm->sp] = a + b;
break;
}
case OP_SUB: {
if (vm->sp < 1) {
fprintf(stderr, "Error: stack underflow\n");
return -1;
}
int b = vm->stack[vm->sp--];
int a = vm->stack[vm->sp--];
vm->stack[++vm->sp] = a - b;
break;
}
case OP_MUL: {
if (vm->sp < 1) {
fprintf(stderr, "Error: stack underflow\n");
return -1;
}
int b = vm->stack[vm->sp--];
int a = vm->stack[vm->sp--];
vm->stack[++vm->sp] = a * b;
break;
}
case OP_DIV: {
if (vm->sp < 1) {
fprintf(stderr, "Error: stack underflow\n");
return -1;
}
int b = vm->stack[vm->sp--];
int a = vm->stack[vm->sp--];
if (b == 0) {
fprintf(stderr, "Error: division by zero\n");
return -1;
}
vm->stack[++vm->sp] = a / b;
break;
}
case OP_EQ: {
if (vm->sp < 1) {
fprintf(stderr, "Error: stack underflow\n");
return -1;
}
int b = vm->stack[vm->sp--];
int a = vm->stack[vm->sp--];
vm->stack[++vm->sp] = (a == b) ? 1 : 0;
break;
}
case OP_NEQ: {
if (vm->sp < 1) {
fprintf(stderr, "Error: stack underflow\n");
return -1;
}
int b = vm->stack[vm->sp--];
int a = vm->stack[vm->sp--];
vm->stack[++vm->sp] = (a != b) ? 1 : 0;
break;
}
case OP_LT: {
if (vm->sp < 1) {
fprintf(stderr, "Error: stack underflow\n");
return -1;
}
int b = vm->stack[vm->sp--];
int a = vm->stack[vm->sp--];
vm->stack[++vm->sp] = (a < b) ? 1 : 0;
break;
}
case OP_LTE: {
if (vm->sp < 1) {
fprintf(stderr, "Error: stack underflow\n");
return -1;
}
int b = vm->stack[vm->sp--];
int a = vm->stack[vm->sp--];
vm->stack[++vm->sp] = (a <= b) ? 1 : 0;
break;
}
case OP_GT: {
if (vm->sp < 1) {
fprintf(stderr, "Error: stack underflow\n");
return -1;
}
int b = vm->stack[vm->sp--];
int a = vm->stack[vm->sp--];
vm->stack[++vm->sp] = (a > b) ? 1 : 0;
break;
}
case OP_GTE: {
if (vm->sp < 1) {
fprintf(stderr, "Error: stack underflow\n");
return -1;
}
int b = vm->stack[vm->sp--];
int a = vm->stack[vm->sp--];
vm->stack[++vm->sp] = (a >= b) ? 1 : 0;
break;
}
case OP_ODD: {
if (vm->sp < 0) {
fprintf(stderr, "Error: stack underflow\n");
return -1;
}
int a = vm->stack[vm->sp--];
vm->stack[++vm->sp] = (a % 2 != 0) ? 1 : 0;
break;
}
case OP_READ: {
int value;
if (scanf("%d", &value) != 1) {
fprintf(stderr, "Error: invalid input\n");
return -1;
}
vm->stack[instr->value] = value;
break;
}
case OP_WRITE: {
if (vm->sp < 0) {
fprintf(stderr, "Error: stack underflow\n");
return -1;
}
printf("%d\n", vm->stack[vm->sp--]);
break;
}
case OP_JMP:
vm->pc = instr->value;
break;
case OP_JZ: {
if (vm->sp < 0) {
fprintf(stderr, "Error: stack underflow\n");
return -1;
}
int condition = vm->stack[vm->sp--];
if (condition == 0) {
vm->pc = instr->value;
}
break;
}
case OP_CALL: {
/* Push return address */
if (vm->sp >= MAX_STACK_SIZE - 1) {
fprintf(stderr, "Error: stack overflow\n");
return -1;
}
vm->stack[++vm->sp] = vm->pc;
vm->pc = instr->value;
break;
}
case OP_RET: {
if (vm->sp < 0) {
fprintf(stderr, "Error: stack underflow\n");
return -1;
}
vm->pc = vm->stack[vm->sp--];
break;
}
case OP_HALT:
return 0;
default:
fprintf(stderr, "Error: unknown opcode %d\n", instr->op);
return -1;
}
}
fprintf(stderr, "Error: unexpected end of program\n");
return -1;
}
/*
* Dump VM state for debugging.
*/
void vm_dump(VM *vm)
{
printf("VM State:\n");
printf(" PC: %d\n", vm->pc);
printf(" SP: %d\n", vm->sp);
printf(" Stack: [");
for (int i = 0; i <= vm->sp; i++) {
printf("%d", vm->stack[i]);
if (i < vm->sp)
printf(", ");
}
printf("]\n");
}
/*
* Run compiled PL/0 program.
*/
int run(Code *code, int trace)
{
VM vm;
vm_init(&vm, code->code, code->code_count);
vm_set_trace(&vm, trace);
return vm_run(&vm);
}