-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvm.c
More file actions
596 lines (547 loc) · 14.7 KB
/
vm.c
File metadata and controls
596 lines (547 loc) · 14.7 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
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
/* ***************************************************************************
* This file is part of virtual_mlv.
*
* virtual_mlv is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published
* by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* virtual_mlv is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with virtual_mlv. If not, see
* <http://www.gnu.org/licenses/>.
*
*
* Authors: S. Lombardy, N. Bedon, C. Morvan, W. Hay, Q. Campos, J. Mangue,
* C. Noël.
*
*************************************************************************** */
#include "stack.h"
#include "vm.h"
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
/** The runtime input for commands which have to read arguments. */
static FILE *input;
/** The first register. */
static int reg1;
/** The second register. */
static int reg2;
/** The program counter. */
static int prog_counter;
/** The base register. */
int base = 0;
/** The code itself. */
int *prog = NULL;
/** The debug flag. */
int debug = 0;
static int vm_nop(void);
static int vm_neg(void);
static int vm_add(void);
static int vm_sub(void);
static int vm_mul(void);
static int vm_div(void);
static int vm_mod(void);
static int vm_equal(void);
static int vm_noteq(void);
static int vm_less(void);
static int vm_leq(void);
static int vm_greater(void);
static int vm_geq(void);
static int vm_set(int);
static int vm_load(void);
static int vm_loadr(void);
static int vm_save(void);
static int vm_saver(void);
static int vm_swap(void);
static int vm_read(void);
static int read_int(int *);
static int vm_write(void);
static int vm_readch(void);
static int vm_writech(void);
static int vm_jump(int);
static int vm_jumpf(int);
static int vm_call(int);
static int vm_return(void);
static int vm_alloc(int);
static int vm_free(int);
static int vm_push(void);
static int vm_pop(void);
static int vm_topst(void);
static int vm_baser(void);
/**
* Does nothing at all.
* @return Always 0.
*/
static int vm_nop(void) {
return 0;
}
/**
* Changes the sign of @p reg1.
* @return Always 0.
*/
static int vm_neg(void) {
reg1 = -reg1;
return 0;
}
/**
* Adds @p reg1 to @p reg2 and stores the result into @p reg1.
* @return Always 0.
*/
static int vm_add(void) {
reg1 = reg1 + reg2;
return 0;
}
/**
* Subtracts @p reg2 from @p reg1 and stores the result into @p reg1.
* @return Always 0.
*/
static int vm_sub(void) {
reg1 = reg1 - reg2;
return 0;
}
/**
* Multiplies @p reg1 by @p reg2 and stores the result into @p reg1.
* @return Always 0.
*/
static int vm_mul(void) {
reg1 = reg1 * reg2;
return 0;
}
/**
* Divides @p reg1 by @p reg2 and stores the result into @p reg1.
* @return 0 upon success or 1 if it was a division by 0.
*/
static int vm_div(void) {
if (reg2 == 0) {
fprintf(stderr, "Floating point exception: division by 0\n");
return 1;
}
reg1 = reg1 / reg2;
return 0;
}
/**
* Computes the remainder of @p reg1 divided by @p reg2.
* @return 0 upon success or 1 if it was a remainder by 0.
*/
static int vm_mod(void) {
if (reg2 == 0) {
fprintf(stderr, "Floating point exception: remainder by 0\n");
return 1;
}
reg1 = reg1 % reg2;
return 0;
}
/**
* Tests if @p reg1 is equal to @p reg2 and stores the result into @p reg1.
* @return Always 0.
*/
static int vm_equal(void) {
reg1 = (reg1 == reg2);
return 0;
}
/**
* Tests if @p reg1 is not equal to @p reg2 and stores the result into @p reg1.
* @return Always 0.
*/
static int vm_noteq(void) {
reg1 = (reg1 != reg2);
return 0;
}
/**
* Tests if @p reg1 is less than @p reg2 and stores the result into @p reg1.
* @return Always 0.
*/
static int vm_less(void) {
reg1 = (reg1 < reg2);
return 0;
}
/**
* Tests if @p reg1 is less than or equal to @p reg2 and stores the result
* into @p reg1.
* @return Always 0.
*/
static int vm_leq(void) {
reg1 = (reg1 <= reg2);
return 0;
}
/**
* Tests if @p reg1 is greater than @p reg2 and stores the result into @p reg1.
* @return Always 0.
*/
static int vm_greater(void) {
reg1 = (reg1 > reg2);
return 0;
}
/**
* Tests if @p reg1 is greater than or equal to @p reg2 and stores the result
* into @p reg1.
* @return Always 0.
*/
static int vm_geq(void) {
reg1 = (reg1 >= reg2);
return 0;
}
/**
* Sets the value of @p reg1.
* @param n The value to be set to @p reg1.
* @return Always 0.
*/
static int vm_set(int n) {
reg1 = n;
return 0;
}
/**
* Loads the value at the stack address @p reg1 into @p reg1.
* @return 0 upon success or 1 if an error occurs.
*/
static int vm_load(void) {
if (dirload(reg1, ®1)) {
fprintf(stderr, "LOAD: illegal stack access\n");
return 1;
}
return 0;
}
/**
* Loads the value at the stack address @p reg1 + @p base into @p reg1.
* @return 0 upon success or 1 if an error occurs.
*/
static int vm_loadr(void) {
if (dirload(reg1 + base, ®1)) {
fprintf(stderr, "LOADR: illegal stack access\n");
return 1;
}
return 0;
}
/**
* Stores the value of @p reg1 at the stack address @p reg2.
* @return 0 upon success or 1 if an error occurs.
*/
static int vm_save(void) {
if (dirsave(reg2, reg1)) {
fprintf(stderr, "SAVE: illegal stack access\n");
return 1;
}
return 0;
}
/**
* Stores the value of @p reg1 at the stack address @p reg2 + @p base.
* @return 0 upon success or 1 if an error occurs.
*/
static int vm_saver(void) {
if (dirsave(reg2 + base, reg1)) {
fprintf(stderr, "SAVER: illegal stack access\n");
return 1;
}
return 0;
}
/**
* Places the value of the base register in reg1.
* @return Always 0.
*/
static int vm_baser(void) {
reg1 = base;
return 0;
}
/**
* Swaps values of @p reg1 and @p reg2.
* @return Always 0.
*/
static int vm_swap(void) {
reg1 ^= reg2;
reg2 ^= reg1;
reg1 ^= reg2;
return 0;
}
/**
* Reads a number from the standard input and stores it into @p reg1.
* @return 0 upon success or 1 if an error occurs.
*/
static int vm_read(void) {
printf("Read: ");
return read_int(®1);
}
/**
* Reads an integer from the standard input.
* @param result The storage location of the integer to be read.
* @return 0 upon success or 1 if an error occurs.
*/
static int read_int(int *result) {
long val;
char *endptr;
char str[BUFSIZ] = {0};
if (fgets(str, sizeof(str), input) == NULL) {
perror("fgets");
return 1;
}
/* Remove the trailing '\n'. */
str[strlen(str) - 1] = '\0';
/* To distinguish success/failure after call. */
errno = 0;
val = strtol(str, &endptr, 10);
/* Check for various possible errors, see strtol(3) for details. */
if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN))
|| (errno != 0 && val == 0)) {
perror("strtol");
return 1;
}
if (endptr == str) {
fprintf(stderr, "Number format error: no digits were found\n");
return 1;
}
/*
* This is not necessarily an error, but we do not want to accept anything
* after the number even if it is legal for strtol().
*/
if (*endptr) {
fprintf(stderr, "Number format error: trailing characters detected\n");
return 1;
}
/* Check for possible overflow because the VM only uses int. */
if (val < INT_MIN || val > INT_MAX) {
fprintf(stderr, "Number format error: result out of an `int' range\n");
return 1;
}
*result = (int) val;
return 0;
}
/**
* Displays the value of @p reg1 to the standard output.
* @return Always 0.
*/
static int vm_write(void) {
printf("Write: %d\n", reg1);
return 0;
}
/**
* Reads a character from the standard input and stores its value into @p reg1.
* @return Always 0.
*/
static int vm_readch(void) {
reg1 = fgetc(input);
return 0;
}
/**
* Writes the value of @p reg1 to the standard output as a character.
* @return Always 0.
*/
static int vm_writech(void) {
putchar(reg1);
return 0;
}
/**
* Unconditionally jumps to a label.
* @param n The label to jump to.
* @return 0 upon success or 1 if the label is out of bounds.
*/
static int vm_jump(int n) {
if (n >= prog_length) {
fprintf(stderr, "JUMP: jump label is out of bounds\n");
return 1;
}
prog_counter = n;
return 0;
}
/**
* Jumps to a label if and only if @p reg1 is false (i.e. equal to 0).
* @param n The label to jump to.
* @return 0 upon success or 1 if the label is out of bounds.
*/
static int vm_jumpf(int n) {
if (reg1 == 0) {
if (n >= prog_length) {
fprintf(stderr, "JUMPF: illegal jump\n");
return 1;
}
prog_counter = n;
}
return 0;
}
/**
* Saves the VM state and then jumps to the call label.
* @param n The call label to jump to.
* @return 0 upon success or 1 if the call label is out of bounds.
*/
static int vm_call(int n) {
/*
* Push the position of the instruction following the call (prog_counter is
* already on that position).
*/
if (push(prog_counter)) {
fprintf(stderr, "Stack overflow\n");
return 1;
}
if (push(base)) {
fprintf(stderr, "Stack overflow\n");
return 1;
}
base = stack_size();
return vm_jump(n);
}
/**
* Pops off all previously pushed values and then restores @p base and
* @p prog_counter.
* @return 0 upon success or 1 if a @a pop fails.
*/
static int vm_return(void) {
while (stack_size() > base) {
if (pop(NULL)) {
fprintf(stderr, "RETURN: stack empty\n");
return 1;
}
}
pop(&base);
pop(&prog_counter);
return 0;
}
/**
* Allocates memory in the stack.
* @param n The size of the memory allocation.
* @return 0 upon success or 1 if a stack overflow occurs.
*/
static int vm_alloc(int n) {
if (extend_stack(n)) {
fprintf(stderr, "ALLOC: stack overflow\n");
return 1;
}
return 0;
}
/**
* Frees memory allocated in the stack.
* @param n The size of the memory deallocation.
* @return 0 upon success or 1 if the stack is empty.
*/
static int vm_free(int n) {
if (reduce_stack(n)) {
fprintf(stderr, "FREE: stack empty\n");
return 1;
}
return 0;
}
/**
* Pushes the value of @p reg1 onto the stack.
* @return 0 upon success or 1 if a stack overflow occurs.
*/
static int vm_push(void) {
if (push(reg1)) {
fprintf(stderr, "PUSH: Stack overflow\n");
return 1;
}
return 0;
}
/**
* Pops a value from the stack and stores it into @p reg1.
* @return 0 upon success or 1 if the stack is empty.
*/
static int vm_pop(void) {
if (pop(®1)) {
fprintf(stderr, "FREE: Stack empty\n");
return 1;
}
return 0;
}
/**
* Puts the current position of top of the stack into @p reg1.
* @return Always 0.
*/
static int vm_topst(void) {
return vm_set(stack_size());
}
/**
* Selects the input which guarantees user interaction at runtime.
* @return 0 upon success or 1 if the input selection failed.
*/
int vm_select_input(void) {
input = isatty(STDIN_FILENO) ? stdin : fopen("/dev/tty", "r");
if (!input) {
perror("fopen");
return 1;
}
return 0;
}
/**
* Halts the VM.
* @return Always 0.
*/
int vm_halt(void) {
free(prog);
free_stack();
if (input != stdin && input != NULL && fclose(input) == EOF) {
perror("fclose");
return 1;
}
return 0;
}
/**
* Executes the code.
* @return 0 upon success or 1 if the program contains illegal opcodes.
*/
int vm_execute(void) {
int halt;
for (halt = 0; !halt && prog_counter < prog_length; /* No increment. */) {
switch (prog[prog_counter++]) {
case VM_NOP: vm_nop(); break;
case VM_NEG: vm_neg(); break;
case VM_ADD: vm_add(); break;
case VM_SUB: vm_sub(); break;
case VM_MUL: vm_mul(); break;
case VM_DIV: vm_div(); break;
case VM_MOD: vm_mod(); break;
case VM_EQUAL: vm_equal(); break;
case VM_NOTEQ: vm_noteq(); break;
case VM_LESS: vm_less(); break;
case VM_LEQ: vm_leq(); break;
case VM_GREATER: vm_greater(); break;
case VM_GEQ: vm_geq(); break;
case VM_SET: vm_set(prog[prog_counter++]); break;
case VM_LOAD: vm_load(); break;
case VM_LOADR: vm_loadr(); break;
case VM_SAVE: vm_save(); break;
case VM_SAVER: vm_saver(); break;
case VM_JUMP: vm_jump(prog[prog_counter++]); break;
case VM_JUMPF: vm_jumpf(prog[prog_counter++]); break;
case VM_PUSH: vm_push(); break;
case VM_POP: vm_pop(); break;
case VM_SWAP: vm_swap(); break;
case VM_READ: vm_read(); break;
case VM_WRITE: vm_write(); break;
case VM_READCH: vm_readch(); break;
case VM_WRITECH: vm_writech(); break;
case VM_HALT: halt = 1; break;
case VM_CALL: vm_call(prog[prog_counter++]); break;
case VM_RETURN: vm_return(); break;
case VM_ALLOC: vm_alloc(prog[prog_counter++]); break;
case VM_FREE: vm_free(prog[prog_counter++]); break;
case VM_TOPST: vm_topst(); break;
case VM_BASER: vm_baser(); break;
/*
* These values cannot be loaded into our program nor be really matched.
* However, they need to remain within the switch for two reasons.
* Firstly because the compiler does not accept unmatched enum values
* and secondly because once these two values are matched, the compiler
* can warn us about real opcodes that are left unmatched.
*/
case VM_LABEL: case __VM_RESERVED:
fprintf(stderr, "Fatal error: illegal opcode encountered\n");
return 1;
/*
* No default case to let the compiler ensure that all opcodes values
* are matched.
*/
}
if (debug) {
display_stack();
printf("#%d reg1 = %d reg2 = %d\n", prog_counter - 1, reg1, reg2);
/* For step-by-step execution. */
fgetc(input);
}
}
return 0;
}