-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvirtual_machine.h
93 lines (73 loc) · 2.25 KB
/
virtual_machine.h
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
#pragma once
#ifndef _VIRTUAL_MACHINE_H_
#define _VIRTUAL_MACHINE_H_
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<time.h>
#include<memory.h>
#include "global_variable.h"
enum {
IMM, LC, LI, SC, SI, PUSH, JMP, JZ, JNZ, CALL, ENT, ADJ, LEV, LEA,
OR, AND, NOT, EQ, NE, LT, LE, GT, GE, ADD, SUB, MUL, DIV, MOD,
EXIT, INI, INR, INS, OUTI, OUTR, OUTS, ASSIGN,
};
int eval()
{
int op, *tmp;
while (true)
{
op = *pc++;
if (op == '\0')break;
/* MOV */
if (op == IMM) { ax = *pc++; }
else if (op == LC) { ax = *(char *)ax; }
else if (op == LI) { ax = *(int *)ax; }
else if (op == SC) { ax = *(char *)*sp++ = ax; }
else if (op == SI) { *(int *)*sp++ = ax; }
/*PUSH*/
else if (op == PUSH) { *--sp = ax; }
/* JMP */
else if (op == JMP) { pc = (int *)*pc; }
/* JZ/JNZ */
else if (op == JZ) { pc = (ax)? pc + 1 : (int *)*pc; }
else if (op == JNZ) { pc = (!ax) ? pc + 1 : (int *)*pc; }
/* CALL */
else if (op == CALL) { *--sp = (int)(pc + 1); pc = (int *)*pc; }
/* ENT */
else if (op == ENT) { *--sp = (int)bp; bp = sp; sp = sp - *pc++; } //? ? ?
/* ADJ */
else if (op == ADJ) { sp = sp + *pc++; }
/* LEV */
else if (op == LEV) { sp = bp; bp = (int *)*sp++; pc = (int *)*sp++; }
/* LEA */
else if (op == LEA) { ax = (int)(bp + *pc++); }
/* operators */
else if (op == OUTR) ax = *sp++ | ax;
else if (op == AND) ax = *sp++ & ax;
else if (op == NOT) ax = ~ax;
else if (op == ASSIGN) *pc++ = ax;
else if (op == EQ) ax = *sp++ == ax;
else if (op == NE) ax = *sp++ != ax;
else if (op == LT) ax = *sp++ < ax;
else if (op == LE) ax = *sp++ <= ax;
else if (op == GT) ax = *sp++ > ax;
else if (op == GE) ax = *sp++ >= ax;
else if (op == ADD) ax = *sp++ + ax;
else if (op == SUB) ax = *sp++ - ax;
else if (op == MUL) ax = *sp++ * ax;
else if (op == DIV) ax = *sp++ / ax;
else if (op == MOD) ax = *sp++ % ax;
/* functions */
else if (op == EXIT) { printf("exit(%d)", *sp); return *sp; }
else if (op == OUTI) printf("%d", *pc++);
else if (op == OUTR)printf("%lf", *pc++);
else if (op == OUTS)printf("%s", *pc++);
else if (op == INI)scanf("%d", (int *)ax);
else if (op == INR)scanf("%lf", (double *)ax);
else if (op == INS)scanf("%s", (char *)ax);
}
return 0;
}
#endif