-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexecute.cc
More file actions
197 lines (182 loc) · 5.53 KB
/
execute.cc
File metadata and controls
197 lines (182 loc) · 5.53 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
/*
* 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"
#include <iostream>
using namespace std;
#define DEBUG 0 // 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;
}
}
}
void printInstructionNode(struct InstructionNode *node, int depth = 100) {
if (node == NULL) {
return;
}
for (int i = 0; i < depth; ++i) {
printf(" "); // Adjust indentation for better visualization
}
switch (node->type) {
case NOOP:
printf("NOOP\n");
break;
case IN:
printf("IN\n");
break;
case OUT:
printf("OUT\n");
break;
case ASSIGN:
printf("ASSIGN: LHS_Index=%d, Operand1_Index=%d, Operand2_Index=%d, Operator=%d\n",
node->assign_inst.left_hand_side_index,
node->assign_inst.opernd1_index,
node->assign_inst.opernd2_index,
node->assign_inst.op);
break;
case CJMP:
printf("CJMP: ConditionOp=%d, Operand1_Index=%d, Operand2_Index=%d\n",
node->cjmp_inst.condition_op,
node->cjmp_inst.opernd1_index,
node->cjmp_inst.opernd2_index);
break;
case JMP:
printf("JMP\n");
break;
default:
printf("Unknown Instruction Type\n");
}
// Recursively traverse the next nodes in the graph
printInstructionNode(node->next, depth);
printInstructionNode(node->jmp_inst.target, depth + 1);
printInstructionNode(node->cjmp_inst.target, depth + 1);
}
void printInstructionNodeGraph(struct InstructionNode *head) {
if (head == NULL) {
printf("Empty graph\n");
return;
}
printf("InstructionNode graph:\n");
printInstructionNode(head, 0);
}
int main()
{
struct InstructionNode *program;
program = parse_Generate_Intermediate_Representation();
execute_program(program);
return 0;
}