-
Notifications
You must be signed in to change notification settings - Fork 0
/
InstrUtils.c
215 lines (205 loc) · 5.4 KB
/
InstrUtils.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
/*
*********************************************
* 314 Principles of Programming Languages *
* Fall 2014 *
*********************************************
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Instr.h"
#include "InstrUtils.h"
#include "Utils.h"
void PrintInstruction(FILE * outfile, Instruction * instr)
{
if (!outfile) {
ERROR("File error\n");
exit(EXIT_FAILURE);
}
if (instr) {
switch (instr->opcode) {
case LOAD:
fprintf(outfile, "LOAD r%d %c\n", instr->field1,
instr->field2);
break;
case LOADI:
fprintf(outfile, "LOADI r%d #%d\n", instr->field1,
instr->field2);
break;
case STORE:
fprintf(outfile, "STORE %c r%d\n", instr->field1,
instr->field2);
break;
case ADD:
fprintf(outfile, "ADD r%d r%d r%d\n", instr->field1,
instr->field2, instr->field3);
break;
case SUB:
fprintf(outfile, "SUB r%d r%d r%d\n", instr->field1,
instr->field2, instr->field3);
break;
case MUL:
fprintf(outfile, "MUL r%d r%d r%d\n", instr->field1,
instr->field2, instr->field3);
break;
case READ:
fprintf(outfile, "READ %c\n", instr->field1);
break;
case WRITE:
fprintf(outfile, "WRITE %c\n", instr->field1);
break;
default:
ERROR("Illegal instructions\n");
}
}
}
void PrintInstructionList(FILE * outfile, Instruction * instr)
{
if (!outfile) {
ERROR("File error\n");
exit(EXIT_FAILURE);
}
if (!instr) {
ERROR("No instructions\n");
exit(EXIT_FAILURE);
}
while (instr) {
PrintInstruction(outfile, instr);
instr = instr->next;
}
}
Instruction *ReadInstruction(FILE * infile)
{
static char InstrBuffer[100];
Instruction *instr = NULL;
char dummy;
if (!infile) {
ERROR("File error\n");
exit(EXIT_FAILURE);
}
instr = (Instruction *) calloc(1, sizeof(Instruction));
if (!instr) {
ERROR("Calloc failed\n");
exit(EXIT_FAILURE);
}
instr->prev = NULL;
instr->next = NULL;
fscanf(infile, "%99s", InstrBuffer);
if (strnlen(InstrBuffer, sizeof(InstrBuffer)) == 0) {
free(instr);
return NULL;
}
if (!strcmp(InstrBuffer, "LOAD")) {
instr->opcode = LOAD;
/* get first operand: register */
fscanf(infile, "%s", InstrBuffer);
sscanf(InstrBuffer, "%c%d", &dummy, &(instr->field1));
/* get second operand: variable */
fscanf(infile, "%s", InstrBuffer);
instr->field2 = *InstrBuffer;
} else if (!strcmp(InstrBuffer, "LOADI")) {
instr->opcode = LOADI;
/* get first operand: register */
fscanf(infile, "%s", InstrBuffer);
sscanf(InstrBuffer, "%c%d", &dummy, &(instr->field1));
/* get second operand: immediate */
fscanf(infile, "%s", InstrBuffer);
sscanf(InstrBuffer, "%c%d", &dummy, &(instr->field2));
} else if (!strcmp(InstrBuffer, "STORE")) {
instr->opcode = STORE;
/* get first operand: variable */
fscanf(infile, "%s", InstrBuffer);
instr->field1 = *InstrBuffer;
/* get second operand: register */
fscanf(infile, "%s", InstrBuffer);
sscanf(InstrBuffer, "%c%d", &dummy, &(instr->field2));
} else if (!strcmp(InstrBuffer, "ADD")) {
instr->opcode = ADD;
/* get first operand: target register */
fscanf(infile, "%s", InstrBuffer);
sscanf(InstrBuffer, "%c%d", &dummy, &(instr->field1));
/* get second operand: register */
fscanf(infile, "%s", InstrBuffer);
sscanf(InstrBuffer, "%c%d", &dummy, &(instr->field2));
/* get third operand: register */
fscanf(infile, "%s", InstrBuffer);
sscanf(InstrBuffer, "%c%d", &dummy, &(instr->field3));
} else if (!strcmp(InstrBuffer, "SUB")) {
instr->opcode = SUB;
/* get first operand: target register */
fscanf(infile, "%s", InstrBuffer);
sscanf(InstrBuffer, "%c%d", &dummy, &(instr->field1));
/* get second operand: register */
fscanf(infile, "%s", InstrBuffer);
sscanf(InstrBuffer, "%c%d", &dummy, &(instr->field2));
/* get third operand: register */
fscanf(infile, "%s", InstrBuffer);
sscanf(InstrBuffer, "%c%d", &dummy, &(instr->field3));
} else if (!strcmp(InstrBuffer, "MUL")) {
instr->opcode = MUL;
/* get first operand: target register */
fscanf(infile, "%s", InstrBuffer);
sscanf(InstrBuffer, "%c%d", &dummy, &(instr->field1));
/* get second operand: register */
fscanf(infile, "%s", InstrBuffer);
sscanf(InstrBuffer, "%c%d", &dummy, &(instr->field2));
/* get third operand: register */
fscanf(infile, "%s", InstrBuffer);
sscanf(InstrBuffer, "%c%d", &dummy, &(instr->field3));
} else if (!strcmp(InstrBuffer, "READ")) {
instr->opcode = READ;
/* get operand: variable */
fscanf(infile, "%s", InstrBuffer);
instr->field1 = *InstrBuffer;
} else if (!strcmp(InstrBuffer, "WRITE")) {
instr->opcode = WRITE;
/* get operand: variable */
fscanf(infile, "%s", InstrBuffer);
instr->field1 = *InstrBuffer;
} else {
free(instr);
return NULL;
}
return instr;
}
Instruction *ReadInstructionList(FILE * infile)
{
Instruction *instr, *head, *tail;
if (!infile) {
ERROR("File error\n");
exit(EXIT_FAILURE);
}
head = tail = NULL;
while ((instr = ReadInstruction(infile))) {
if (!head) {
head = tail = instr;
continue;
}
instr->prev = tail;
instr->next = NULL;
tail->next = instr;
tail = instr;
}
return head;
}
Instruction *LastInstruction(Instruction * instr)
{
if (!instr) {
ERROR("No instructions\n");
exit(EXIT_FAILURE);
}
while (instr->next)
instr = instr->next;
return instr;
}
void DestroyInstructionList(Instruction * instr)
{
Instruction *i;
if (!instr)
return;
while (instr) {
i = instr;
instr = instr->next;
free(i);
}
}