-
Notifications
You must be signed in to change notification settings - Fork 0
/
pmachine.c
360 lines (302 loc) · 7.45 KB
/
pmachine.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
/*
// Name : Pedro Poveda
// PROG: pmachine.c
// Description: Virtual Machine for running P-Code
*/
#include <stdio.h>
#include <stdlib.h>
#define MAX_STACK_HEIGHT = 2000;
#define MAX_CODE_LENGTH = 500;
#define MAX_LEXI_LEVELs = 3;
#define STACK_MAX 100
typedef struct{
int data[STACK_MAX];
int size;
} stack;
//stuff I have to use program wide
typedef struct{
int op; //op code
int l; // L
int m; // M
} instruction;
char *instructionCodes[]= {
"LIT", "OPR","LOD","STO","CAL",
"INC","JMP","JPC", "SIO"
};
void printInstructions(instruction *list , int length, FILE *outputFile);
void virtualize (instruction *list, stack *slist,int length, FILE *outputFile);
void stackPush(stack *s, int d);
int stackPop(stack *s);
void stackEdit(stack *s, int value, int offset);
int stackGet(stack *s, int offset);
void stackMath(stack *s, int op);
void printStack(stack *s, FILE *out);
instruction *instructionList = NULL;
stack *stackList = NULL;
int main(){
stack *stackList = malloc(3*sizeof(stack)); //for each lexigraphical level
//initiate stack
var i;
for(i=0; i < 3; i++){
stackList[i].size=0;
}
FILE *inputFilePTR, *outputFilePTR;
inputFilePTR = fopen("mcode.txt", "r");
outputFilePTR = fopen("stacktrace.txt", "w");
int counter =0;
char line[80];
while (fgets(line, 80, inputFilePTR)) {
//intialize a pointer and keep mallocing and adding stuff to it.
instructionList = (instruction*) realloc(instructionList, (counter + 1)* sizeof(instruction));
sscanf(line, "%d %d %d\n", &instructionList[counter].op, &instructionList[counter].l, &instructionList[counter].m);
counter++;
}
int length = counter++; //not sure if this is messy
//print instructions here
printInstructions(instructionList, length, outputFilePTR);
virtualize(instructionList, stackList, length, outputFilePTR);
//Maintenance section
free(instructionList);
free(stackList);
fclose(inputFilePTR);
fclose(outputFilePTR);
return 0;
}
void printInstructions(instruction *list , int length, FILE *outputFile)
{
fprintf(outputFile,"%.4s\t%.3s\t%.1s\t%.2s\n", "Line","OP", "L", "M" );
int i;
for(i=0; i < length; i++){
fprintf(outputFile,"%2d\t\t%s\t%d\t%d\n", i, instructionCodes[list[i].op-1], list[i].l, list[i].m);
}
fprintf(outputFile,"\n\n");
}
void virtualize (instruction *list, stack *slist,int length, FILE *outputFile)
{
// instantiate default values
int sp, bp, pc;
sp = 0;
bp = 1;
pc = 0;
instruction *ir = NULL;
int breakFlag=0;
int incrementLevelFlag=0;
int currentLevel=0;
//int levelSeperator = 0;
//print out initial values
fprintf(outputFile,"\t\t\t\t%.2s\t%.2s\t%.2s\t%s\n", "pc", "bp","sp","stack");
fprintf(outputFile,"Initial Values\t\t\t%2d\t%2d\t%2d\t\n", pc, bp, sp);
//loop through all instructions and run them
int temp; //temp variable for all operations
while(1){
ir= &list[pc];
//print out the first part of the instruction
fprintf(outputFile,"%2d\t%.3s\t%2d\t%2d\t", pc, instructionCodes[ir->op -1],ir->l, ir->m);
//perform instruction, huge switch statement
switch(ir->op)
{
case 1: //Push value M onto stack *DONE
sp+=1;
stackPush(&slist[currentLevel],ir->m);
pc++;
break;
case 2: //Arithmetic Operation
switch(ir->m){
case 0: // RET Return from function or procedure DONE
sp = bp-1;
pc = stackGet(&slist[currentLevel], 3);
bp = stackGet(&slist[currentLevel], 2);
currentLevel--;
break;
case 1:
temp = stackPop(&slist[currentLevel]);
stackPush(&slist[currentLevel], -1*temp);
pc++; //DONE
break;
case 7:
temp = stackPop(&slist[currentLevel]);
stackPush(&slist[currentLevel], temp%2);
//DONE
pc++;
break;
default: //for Math functions that require popping DONE
stackMath(&slist[currentLevel],ir->m);
sp--;
break;
}
break;
case 3: //Get value in frame L levels down at offset M and push it *DONE
sp++;
temp =stackGet(&slist[currentLevel -ir->l], ir->m);
stackPush(&slist[currentLevel], temp );
break;
case 4:; //Pop stack and insert value in frame L levels down at offset M *DONE
temp= stackPop(&slist[currentLevel]);
stackEdit(&slist[currentLevel -ir->l], temp, ir->m);
pc++;
sp--;
break;
case 5: //Call procedure at M (generates new stack frame) *DONE
//instantiate values in the new stack
stackPush(&slist[currentLevel + 1], 0);
stackPush(&slist[currentLevel + 1], currentLevel +1);
stackPush(&slist[currentLevel +1], bp);
stackPush(&slist[currentLevel +1], ++pc);
bp= sp + 1;
pc = ir->m;
incrementLevelFlag = 1;
break;
case 6:;//Allocate M locals on stack *DONE
int k;
temp = slist[currentLevel].size;
for(k = 0; k < (ir->m - temp); k++){ //this way we don't over-push
stackPush(&slist[currentLevel],0);
}
sp += ir->m;
pc++;
break;
case 7: //Jump to M *DONE
pc = ir->m;
break;
case 8: //Pop stack and jump to M if value is equal to 0 *DONE
if(stackPop(&slist[ir->l]) == 0)
pc= ir->m;
else{
pc++;
}
sp--;
break;
case 9:
switch(ir->m){
case 0: //Pop stack and print out value *DONE
stackPop(&slist[currentLevel]);
sp--;
break;
case 1:; //Read in input from user and push it *DONE
int tempInt;
scanf("%d", &tempInt);
stackPush(&slist[currentLevel], tempInt);
break;
case 2: //Halt the machine *DONE
breakFlag =1;
break;
}
break;
}
//print out contents of stack and stuff to disk.
fprintf(outputFile,"%2d\t%2d\t%2d\t", pc,bp,sp);
//print stacks
int c;
for(c =0; c <= currentLevel; c++)
{
printStack(&slist[c], outputFile);
if(c!= currentLevel)
fprintf(outputFile,"| ");
}
fprintf(outputFile,"\n");
if(breakFlag)
break;
else if(incrementLevelFlag){
currentLevel++;
incrementLevelFlag=0;
}
}
return;
}
void stackPush(stack *s, int d)
{
if (s->size < STACK_MAX)
s->data[s->size++] = d;
}
int stackPop(stack *s)
{
if (s->size == 0){
return -1;
}
else{
s->size--;
return s->data[s->size];
}
}
void stackEdit(stack *s, int value, int offset)
{
s->data[offset] = value;
return;
}
int stackGet(stack *s, int offset)
{
return s->data[offset];
}
void stackMath(stack *s, int op)
{
int temp1; //local temp1 variable
int temp2; //local temp2 variable
switch(op){
case 2: //ADD
temp1 = stackPop(s);
temp2 = stackPop(s);
stackPush(s, temp2+temp1);
break;
case 3: //SUBTRACT
temp1 = stackPop(s);
temp2 = stackPop(s);
stackPush(s, temp2-temp1);
break;
case 4: //Multiply
temp1 = stackPop(s);
temp2 = stackPop(s);
stackPush(s, temp2*temp1);
break;
case 5: //Divide
temp1 = stackPop(s);
temp2 = stackPop(s);
stackPush(s, temp2/temp1);
break;
case 7: //MOD
temp1 = stackPop(s);
temp2 = stackPop(s);
stackPush(s, temp2%temp1);
break;
case 8: //EQL
temp1 = stackPop(s);
temp2 = stackPop(s);
stackPush(s, temp2==temp1);
break;
case 9: //NEQ
temp1 = stackPop(s);
temp2 = stackPop(s);
stackPush(s, temp2!=temp1);
break;
case 10: //LSS
temp1 = stackPop(s);
temp2 = stackPop(s);
stackPush(s, temp2 < temp1);
break;
case 11: //LEQ
temp1 = stackPop(s);
temp2 = stackPop(s);
stackPush(s, temp2 <= temp1);
break;
case 12: //GTR
temp1 = stackPop(s);
temp2 = stackPop(s);
stackPush(s, temp2 > temp1);
break;
case 13: //GEQ
temp1 = stackPop(s);
temp2 = stackPop(s);
stackPush(s, temp2 >= temp1);
break;
}
return;
}
void printStack(stack *s, FILE *out)
{
if (s->size != 0) {
int i;
for(i = 0; i < s->size; i++){
fprintf(out,"%d ", s->data[i]);
}
}
return;
}