-
Notifications
You must be signed in to change notification settings - Fork 0
/
vm.c
442 lines (382 loc) · 11.3 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
// Compiler Builder 25
// Elizabeth Maspoch
// Hemali Mistry
// Kevin Negy
// Courtney Tennant
#include <stdio.h>
#include <stdlib.h>
struct instruction{
int op; // opcode
int l; // L
int m; // M
} ;
void printstatus();
int base( int level, int b );
void LIT(struct instruction ir);
void OPR(struct instruction ir);
void LOD(struct instruction ir);
void STO(struct instruction ir);
void CAL(struct instruction ir);
void INC(struct instruction ir);
void JMP(struct instruction ir);
void JPC(struct instruction ir);
int SIO(struct instruction ir);
#define MAX_STACK_HEIGHT 2000
#define MAX_CODE_LENGTH 500
#define MAX_LEXT_LEVELS 3
// Declare sp, bp, pc and the stack array
int sp = 0;
int bp = 1;
int pc = 0;
int stack[MAX_STACK_HEIGHT]={0};
int main(int argc, char * argv[]){
if(argc != 2){
printf("./vm <input>\n");
return -1;
}
printf("PL/0 code:\n\n");
// Create the ir instruction register and an array of instructions
struct instruction ir;
struct instruction *code=malloc(sizeof(struct instruction)*MAX_CODE_LENGTH);
int i=0;
// Open command line argument file to get instructions
FILE *fp;
fp=fopen(argv[1],"r");
// Until the end of file is reached, scan in instructions into code array
while(!feof(fp)){
fscanf(fp,"%d %d %d",&code[i].op, &code[i].l,&code[i].m);
// Print out appropriate instructions in order and including relevant information
switch(code[i].op){
case 1:
printf("%3d LIT %3d \n",i, code[i].m);
break;
case 2:{
switch (code[i].m){
case 0:{
printf("%3d RET \n", i);
break;
}
case 1:{
printf("%3d NEG \n", i);
break;
}
case 2:{
printf("%3d ADD \n", i);
break;
}
case 3:{
printf("%3d SUB \n", i);
break;
}
case 4:{
printf("%3d MUL \n", i);
break;
}
case 5:{
printf("%3d DIV \n", i);
break;
}
case 6:{
printf("%3d ODD \n", i);
break;
}
case 7:{
printf("%3d MOD \n", i);
break;
}
case 8:{
printf("%3d EQL \n", i);
break;
}
case 9:{
printf("%3d NEQ \n", i);
break;
}
case 10:{
printf("%3d LSS \n", i);
break;
}
case 11:{
printf("%3d LEQ \n", i);
break;
}
case 12:{
printf("%3d GTR \n", i);
break;
}
case 13:{
printf("%3d GEQ \n", i);
break;
}
}
break;
}
case 3:
printf("%3d LOD %d %3d \n",i, code[i].l, code[i].m);
break;
case 4:
printf("%3d STO %d %3d \n", i,code[i].l, code[i].m);
break;
case 5:
printf("%3d CAL %d %3d \n",i,code[i].l, code[i].m);
break;
case 6:
printf("%3d INC %3d \n",i, code[i].m);
break;
case 7:
printf("%3d JMP %3d \n",i, code[i].m);
break;
case 8:
printf("%3d JPC %3d \n", i, code[i].m);
break;
case 9:{
if (code[i].m==0){
printf("%3d OUT %3d \n", i, code[i].m);
}
else if (code[i].m==1){
printf("%3d INP %3d \n", i, code[i].m);
}
else{
printf("%3d HLT \n", i);
}
break;
}
}
i++;
}
printf("\nExecution:\n");
printf(" pc bp sp stack\n");
printf(" %2d %2d %2d ", pc,bp,sp);
while(1){
// Fetch Cycle
ir=code[pc];
pc++;
printf("\n");
// Execution Cycle
if(ir.op==1)
LIT(ir);
else if(ir.op==2)
OPR(ir);
else if(ir.op==3)
LOD(ir);
else if(ir.op==4)
STO(ir);
else if (ir.op==5){
CAL(ir);
//getchar();
}
else if(ir.op==6)
INC(ir);
else if (ir.op==7)
JMP(ir);
else if(ir.op==8){
JPC(ir);
}
else if(ir.op==9){
// If SIO returns 1, halt is executed. Break out of switch
if(SIO(ir)==1)
break;
}
}
printf("\n");
return 0;
}
// Calculates the bp L levels up from the current bp
int base( int level, int b ) {
while (level > 0) {
b = stack[ b + 1 ];
level--;
}
// Stack array does not use 0 index, if calculation results in b==0, increment to 1
if(b==0)
b++;
return b;
}
// Pushes a value(ir.m) onto the stack
void LIT(struct instruction ir){
printf("%3d LIT %3d ", pc-1, ir.m);
sp+=1;
stack[sp]=ir.m;
printstatus();
}
// Gets value at offset m in frame l levels down and pushes onto stack
void LOD(struct instruction ir){
//printf("%d\n", base(ir.l,bp)+ir.m);
printf("%3d LOD %2d %3d ", pc-1,ir.l,ir.m);
sp+=1;
stack[sp]=stack[base(ir.l, bp)+ir.m];
printstatus();
}
// Pops stack and stores value at offset M, L levels down the stack
void STO(struct instruction ir){
printf("%3d STO %2d %3d ", pc-1,ir.l,ir.m);
stack[base(ir.l, bp)+ir.m]=stack[sp];
sp-=1;
printstatus();
}
// Creates a new stack frame (essentially a new sub function)
void CAL(struct instruction ir){
stack[sp+1]=0;
stack[sp+2]=base(ir.l,bp);
stack[sp+3]=bp;
stack[sp+4]=pc;
printf("%3d CAL %2d ", pc-1,ir.l);
pc=ir.m;
bp=sp+1;
printf("%3d ",pc);
printstatus();
}
// Allocate M local variables on the stack
void INC(struct instruction ir){
printf("%3d INC %3d ", pc-1,ir.m);
sp+=ir.m;
printstatus();
}
int SIO(struct instruction ir){
// OUT function pops stack and prints out value
if (ir.m==0){
printf("%3d OUT ", pc-1);
sp--;
printstatus();
printf("\n%d",stack[sp+1]);
return 0;
}
// INP reads input from user and pushes onto stack
if (ir.m==1){
printf("%3d INP ", pc-1);
sp++;
scanf("%d",&stack[sp]);
printstatus();
printf("\nread %d from input",stack[sp]);
return 0;
}
// HALT ends the program
if(ir.m==2){
printf("%3d HLT ", pc-1);
printstatus();
return 1;
}
}
// Changes PC to ir.m so that next fetch cycle "jumps" to desired instruction
void JMP(struct instruction ir){
printf("%3d JMP %3d ", pc-1,ir.m);
pc=ir.m;
printstatus();
}
// Pops stack and sets pc to ir.m if the value popped==0
void JPC(struct instruction ir) {
printf("%3d JPC %3d ", pc-1, ir.m);
if (stack [sp] == 0)
pc = ir.m;
sp--;
printstatus();
}
// Prints out pc, bp,sp, and the current stack
void printstatus(){
printf("%3d %2d %2d ", pc,bp,sp);
int i,j;
if(sp!=0){
// ignore sp[0], start at 1
for(i=1;i<=sp;i++){
printf("%d ",stack[i]);
int tempbp=1,level=0;
while(level<=MAX_LEXT_LEVELS){
tempbp=base(level,bp);
if(tempbp==i+1 && i!=sp ){
printf("| ");
break;
}
level++;
}
}
}
}
// All arithmetic functions
void OPR(struct instruction ir){
switch(ir.m){
// RET function used for returning control to parent function
case 0:
printf("%3d RET ", pc-1);
sp = bp - 1;
pc = stack[sp+4];
bp = stack[sp+3];
break;
// NEG function multiplies the top stack value by -1
case 1:
printf("%3d NEG ", pc-1);
stack[sp] = -stack[sp];
break;
// Adds top two values in the stack and places result on stack
case 2:
printf("%3d ADD ", pc-1);
sp=sp-1;
stack[sp] = stack[sp] + stack[sp+1];
break;
// Subtract function
case 3:
printf("%3d SUB ", pc-1);
sp=sp-1;
stack[sp] = stack[sp] - stack[sp+1];
break;
// Multiplication function
case 4:
printf("%3d MUL ", pc-1);
sp=sp-1;
stack[sp] = stack[sp] * stack[sp+1];
break;
// Divide function
case 5:
printf("%3d DIV ", pc-1);
sp=sp-1;
stack[sp] = stack[sp] / stack[sp+1];
break;
// Pushes 1 onto stack if top of stack is an odd number, 0 if even
case 6:
printf("%3d ODD ", pc-1);
stack[sp] = stack[sp] % 2;
break;
// Mods top two stack values and places result on stack
case 7:
printf("%3d MOD ", pc-1);
sp=sp-1;
stack[sp] = stack[sp] % stack[(sp)+1];
break;
// Checks if top two stack values are equal. 1 if equal, 0 if not equal
case 8:
printf("%3d EQL ", pc-1);
sp=sp-1;
stack[sp] = stack[sp] == stack[sp+1];
break;
// NEQ function checks that top two values are not equal
case 9:
printf("%3d NEQ ", pc-1);
sp=sp-1;
stack[sp] = stack[sp] != stack[sp+1];
break;
// If penultimate value on stack less than top of stack, set top to 1, else 0
case 10:
printf("%3d LSS ", pc-1);
sp=sp-1;
stack[sp] = stack[sp] < stack[sp+1];
break;
// If penultimate value on stack less than or equal top of stack, set top to 1, else 0
case 11:
printf("%3d LEQ ", pc-1);
sp=sp-1;
stack[sp] = stack[sp] <= stack[sp+1];
break;
// If penultimate value on stack greater than top of stack, set top to 1, else 0
case 12:
printf("%3d GTR ", pc-1);
sp=sp-1;
stack[sp] = stack[sp] > stack[sp+1];
break;
// If penultimate value on stack greater than or equal top of stack, set top to 1, else 0
case 13:
printf("%3d GEQ ", pc-1);
sp=sp-1;
stack[sp] = stack[sp] >= stack[sp+1];
break;
}
printstatus();
}