-
Notifications
You must be signed in to change notification settings - Fork 0
/
virtual_machine.c
292 lines (239 loc) · 10.3 KB
/
virtual_machine.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
#include <stdio.h>
#include <stdlib.h>
#include "virtual_machine.h"
/* Starts and stops the program */
int halt = -1;
/* Process Address Space */
int process_address_space[MAX_PROGRAM_SIZE];
/* REGISTERS */
int base_pointer;
int stack_pointer;
int program_counter;
/* Instruction Register */
int instruction_size;
instruction instruction_register;
/* Activation Records */
int record_size = 0;
activation_record records[MAX_ACTIVATION_RECORD_SIZE];
char *opCodeName(int operation)
{
if(operation == 1) return "LIT";
else if(operation == 2) return operationName(instruction_register.m_address);
else if(operation == 3) return "LOD";
else if(operation == 4) return "STO";
else if(operation == 5) return "CAL";
else if(operation == 6) return "INC";
else if(operation == 7) return "JMP";
else if(operation == 8) return "JCP";
else if(operation == 9) return "SYS";
else return NULL;
}
char *operationName(int operation)
{
if(operation == 0) return "RTN";
else if(operation == 1) return "ADD";
else if(operation == 2) return "SUB";
else if(operation == 3) return "MUL";
else if(operation == 4) return "DIV";
else if(operation == 5) return "EQL";
else if(operation == 6) return "NEQ";
else if(operation == 7) return "LSS";
else if(operation == 8) return "LEQ";
else if(operation == 9) return "GTR";
else if(operation == 10) return "GEQ";
else return NULL;
}
/* Find base level */
int findBaseLevel(int bp, int level)
{
int activation_record_base = bp;
while(level > 0)
{
activation_record_base = process_address_space[activation_record_base];
level--;
}
return activation_record_base;
}
void fetch()
{
instruction_register.opCode = process_address_space[program_counter];
instruction_register.lex_level = process_address_space[program_counter + 1];
instruction_register.m_address = process_address_space[program_counter + 2];
program_counter = program_counter + 3;
}
void execute()
{
switch (instruction_register.opCode)
{
case 1: // Pushes a constant value (literal) M onto the stack
stack_pointer = stack_pointer + 1;
process_address_space[stack_pointer] = instruction_register.m_address;
break;
case 2: // Operation to be performed on the data at the top of the stack. (or return from function)
switch (instruction_register.m_address)
{
case 0: // RTN operation
stack_pointer = base_pointer - 1;
base_pointer = process_address_space[stack_pointer + 2];
program_counter = process_address_space[stack_pointer + 3];
record_size = record_size - 1;
break;
case 1: // ADD operation
process_address_space[stack_pointer - 1] =
process_address_space[stack_pointer - 1] + process_address_space[stack_pointer];
stack_pointer = stack_pointer - 1;
break;
case 2: // SUB operation
process_address_space[stack_pointer - 1] =
process_address_space[stack_pointer - 1] - process_address_space[stack_pointer];
stack_pointer = stack_pointer - 1;
break;
case 3: // MUL operation
process_address_space[stack_pointer - 1] =
process_address_space[stack_pointer - 1] * process_address_space[stack_pointer];
stack_pointer = stack_pointer - 1;
break;
case 4: // DIV operation
process_address_space[stack_pointer - 1] =
process_address_space[stack_pointer - 1] / process_address_space[stack_pointer];
stack_pointer = stack_pointer - 1;
break;
case 5: // EQL operation
process_address_space[stack_pointer - 1] =
process_address_space[stack_pointer - 1] == process_address_space[stack_pointer];
stack_pointer = stack_pointer - 1;
break;
case 6: // NEQ operation
process_address_space[stack_pointer - 1] =
process_address_space[stack_pointer - 1] != process_address_space[stack_pointer];
stack_pointer = stack_pointer - 1;
break;
case 7: // LSS operation
process_address_space[stack_pointer - 1] =
process_address_space[stack_pointer - 1] < process_address_space[stack_pointer];
stack_pointer = stack_pointer - 1;
break;
case 8: // LEQ operation
process_address_space[stack_pointer - 1] =
process_address_space[stack_pointer - 1] <= process_address_space[stack_pointer];
stack_pointer = stack_pointer - 1;
break;
case 9: // GTR operation
process_address_space[stack_pointer - 1] =
process_address_space[stack_pointer - 1] > process_address_space[stack_pointer];
stack_pointer = stack_pointer - 1;
break;
case 10: // GEQ operation
process_address_space[stack_pointer - 1] =
process_address_space[stack_pointer - 1] >= process_address_space[stack_pointer];
stack_pointer = stack_pointer - 1;
break;
default:
break;
}
break;
case 3: // Load value to top of stack from the stack location at offset M in AR located L lexicographical levels down
stack_pointer = stack_pointer + 1;
process_address_space[stack_pointer] =
process_address_space[findBaseLevel(base_pointer, instruction_register.lex_level) + instruction_register.m_address];
break;
case 4: // Store value at top of stack in stack location at offset M in AR located L lexicographical levels down
process_address_space[findBaseLevel(base_pointer, instruction_register.lex_level) + instruction_register.m_address] =
process_address_space[stack_pointer];
stack_pointer = stack_pointer - 1;
break;
case 5: // Call procedure at code index M (generates new Activation Record and PC <- M)
process_address_space[stack_pointer + 1] = findBaseLevel(base_pointer, instruction_register.lex_level);
process_address_space[stack_pointer + 2] = base_pointer;
process_address_space[stack_pointer + 3] = program_counter;
base_pointer = stack_pointer + 1;
program_counter = instruction_register.m_address;
record_size = record_size + 1;
break;
case 6: // Allocates M memory words (increment SP by M)
stack_pointer = stack_pointer + instruction_register.m_address;
break;
case 7: // Jump to instruction M (PC <- M)
program_counter = instruction_register.m_address;
break;
case 8: // Jump to instruction M if topstack element is 0
if(process_address_space[stack_pointer] == 0) program_counter = instruction_register.m_address;
stack_pointer = stack_pointer - 1;
break;
case 9: // System operations
switch (instruction_register.m_address)
{
case 1: // Write the top stack element to the screen
fprintf(stderr, "Output result is: %2d\n", process_address_space[stack_pointer]);
stack_pointer = stack_pointer - 1;
break;
case 2: // Read in input from the user and store it on top of the stack
stack_pointer = stack_pointer + 1;
fprintf(stderr, "Please Enter an Intger: ");
scanf("%d", &process_address_space[stack_pointer]);
break;
case 3: // End of program (Set halt flag to zero)
halt = 0;
break;
default:
break;
}
default:
break;
}
records[record_size].base_pointer = base_pointer;
records[record_size].stack_pointer = stack_pointer;
}
void readELF(char *fileName)
{
FILE *input_file = fopen(fileName, "r");
instruction_size = 0;
do {
fscanf(input_file, "%d %d %d", &process_address_space[instruction_size],
&process_address_space[instruction_size + 1], &process_address_space[instruction_size + 2]);
instruction_size = instruction_size + 3;
} while(feof(input_file) == 0);
fclose(input_file);
}
void virtualMachine()
{
base_pointer = instruction_size;
stack_pointer = base_pointer - 1;
program_counter = 0;
fprintf(stderr, "\t\t PC BP SP stack\n");
fprintf(stderr, "Initial values: %3d %3d %3d\n\n", program_counter, base_pointer, stack_pointer);
while(halt != 0)
{
/* During the fetch cycle, data stored in memory is retrieved and saved in instruction register */
fetch();
/* During the execution cycle, data in the instruction register are executed */
execute();
/* Outputs the result of the execution in the console */
fprintf(stderr, " %3s %3d %3d", opCodeName(instruction_register.opCode), instruction_register.lex_level, instruction_register.m_address);
fprintf(stderr, " %3d %3d %3d ", program_counter, base_pointer, stack_pointer);
//fprintf(stderr, "Num Records: %d ", num_records);
for(int i = 0; i <= record_size; i++)
{
for(int j = records[i].base_pointer; j <= records[i].stack_pointer; j++)
{
fprintf(stderr, "%d ", process_address_space[j]);
}
if(i != record_size) fprintf(stderr, "| ");
}
fprintf(stderr, "\n");
}
}
int main(int argc, char *argv[])
{
if(argc < 2)
{
printf("Filename missing!\n");
exit(1);
}
else
{
readELF(argv[1]);
virtualMachine();
}
return 0;
}