-
Notifications
You must be signed in to change notification settings - Fork 0
/
execute.cc
143 lines (132 loc) · 4.21 KB
/
execute.cc
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
/*
* Copyright (C) Rida Bazzi, 2017-2022
*
* Do not share this file with anyone
*/
#include <cstdio>
#include <cstdlib>
#include <cstdarg>
#include <cctype>
#include <cstring>
#include <string>
#include "execute.h"
using namespace std;
#define DEBUG 1 // 1 => Turn ON debugging, 0 => Turn OFF debugging
int mem[1000];
int next_available = 0;
std::vector<int> inputs;
int next_input = 0;
void debug(const char* format, ...)
{
va_list args;
if (DEBUG)
{
va_start (args, format);
vfprintf (stdout, format, args);
va_end (args);
}
}
void execute_program(struct InstructionNode * program)
{
struct InstructionNode * pc = program;
int op1, op2, result;
while(pc != NULL)
{
switch(pc->type)
{
case NOOP:
pc = pc->next;
break;
case IN:
mem[pc->input_inst.var_index] = inputs[next_input];
next_input++;
pc = pc->next;
break;
case OUT:
printf("%d ", mem[pc->output_inst.var_index]);
fflush(stdin);
pc = pc->next;
break;
case ASSIGN:
switch(pc->assign_inst.op)
{
case OPERATOR_PLUS:
op1 = mem[pc->assign_inst.opernd1_index];
op2 = mem[pc->assign_inst.opernd2_index];
result = op1 + op2;
break;
case OPERATOR_MINUS:
op1 = mem[pc->assign_inst.opernd1_index];
op2 = mem[pc->assign_inst.opernd2_index];
result = op1 - op2;
break;
case OPERATOR_MULT:
op1 = mem[pc->assign_inst.opernd1_index];
op2 = mem[pc->assign_inst.opernd2_index];
result = op1 * op2;
break;
case OPERATOR_DIV:
op1 = mem[pc->assign_inst.opernd1_index];
op2 = mem[pc->assign_inst.opernd2_index];
result = op1 / op2;
break;
case OPERATOR_NONE:
op1 = mem[pc->assign_inst.opernd1_index];
result = op1;
break;
}
mem[pc->assign_inst.left_hand_side_index] = result;
pc = pc->next;
break;
case CJMP:
if (pc->cjmp_inst.target == NULL)
{
debug("Error: pc->cjmp_inst->target is null.\n");
exit(1);
}
op1 = mem[pc->cjmp_inst.opernd1_index];
op2 = mem[pc->cjmp_inst.opernd2_index];
switch(pc->cjmp_inst.condition_op)
{
case CONDITION_GREATER:
if(op1 > op2)
pc = pc->next;
else
pc = pc->cjmp_inst.target;
break;
case CONDITION_LESS:
if(op1 < op2)
pc = pc->next;
else
pc = pc->cjmp_inst.target;
break;
case CONDITION_NOTEQUAL:
if(op1 != op2)
pc = pc->next;
else
pc = pc->cjmp_inst.target;
break;
}
break;
case JMP:
if (pc->jmp_inst.target == NULL)
{
debug("Error: pc->jmp_inst->target is null.\n");
exit(1);
}
pc = pc->jmp_inst.target;
break;
default:
debug("Error: invalid value for pc->type (%d).\n", pc->type);
exit(1);
break;
}
}
}
int main()
{
struct InstructionNode * program;
program = parse_Generate_Intermediate_Representation();
execute_program(program);
return 0;
}