-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.c
More file actions
406 lines (387 loc) · 9.19 KB
/
setup.c
File metadata and controls
406 lines (387 loc) · 9.19 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
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include "schedutils.h"
void init(Instruction* head){
Instruction h = malloc(sizeof(struct Command));
h->opcode = 0;
h->next = NULL;
h->prev = NULL;
h->ismem = -1;
h->firstReg = NULL;
h->secondReg = NULL;
h->outputReg1 = NULL;
h->outputReg2 = NULL;
h->successors = NULL;
h->numInstructions = 0;
h->priority = 0;
h->numdepends = 0;
h->cycle = 0;
h->delay = -1;
h->found = 0;
h->isDone = 0;
h->hasAnti = 0;
h->isReady = 0;
h->hasMemTrue = 0;
h->isActive = 0;
h->isLeaves = 0;
h->ismemread = 0;
h->ismemwrite = 0;
h->memoryDepends = NULL;
*head = h;
}
// Fix this up later
void destroy(Instruction node){
if(node->firstReg != NULL)
free(node->firstReg); // have to free the register stuff if they have stuff inside
if(node->secondReg != NULL)
free(node->secondReg);
if(node->outputReg1 != NULL)
free(node->outputReg1);
if(node->outputReg2 != NULL)
free(node->outputReg2);
free(node->successors);
free(node->memoryDepends);
int i = 0;
for(i = 0; i <5; i++){
if(node->depends[i] != NULL)
free(node->depends[i]);
}
free(node);
}
// Fix this up later
void destroyRep(Instruction head){
//Instruction previous = head->prev;
Instruction temp = head;
//while(previous != NULL){
// previous = previous->prev;
// destroy(previous->next);
//}
while(temp != NULL){
Instruction next = temp->next;
destroy(temp);
temp = next;
//destroy(temp->prev);
}
}
void create(Instruction* node){
Instruction head = NULL;
head = malloc(sizeof(struct Command));
head->opcode = 0;
head->next = NULL;
head->prev = NULL;
head->prev = NULL;
head->ismem = -1;
head->firstReg = NULL;
head->secondReg = NULL;
head->outputReg1 = NULL;
head->outputReg2 = NULL;
head->successors = NULL;
head->numInstructions = 0;
head->priority = 0;
head->numdepends = 0;
head->cycle = 0;
head->delay = -1;
head->found = 0;
head->isDone = 0;
head->hasAnti = 0;
head->isReady = 0;
head->hasMemTrue = 0;
head->isActive = 0;
head->isLeaves = 0;
head->ismemread = 0;
head->ismemwrite = 0;
head->memoryDepends = NULL;
char line [200];
int numlines = 0;
int index = 0;
int rc = 0;
char instr[20], r1[30], r2[30], r3[30], r4[30];
instr[0] = '\0';
r1[0] = '\0';
r2[0] = '\0';
r3[0] = '\0';
r4[0] = '\0';
while( fgets ( line, sizeof line, stdin) != NULL){
//puts(line);
rc = sscanf(line, "%s %s %s %s %s", instr,r1,r2,r3,r4);
chooseOp(head, instr);
createReg(head, r1, 0);
if(rc == 4){
createReg(head, r3, 2);
}
else{
if(r2[0] != '=' && r2[0] != '\0'){
createReg(head, r2, 1);
createReg(head, r4, 3);
}
else if(r3[0] != '=' && r3[0] != '\0'){
createReg(head, r3, 2);
createReg(head, r4, 3);
}
}
Instruction next = NULL;
init(&next);
numlines++;
head->next = next;
next->prev = head;
head = next;
//Point prevs's next to new head, and that heads prev to the previous head.
rc = 0;
r1[0] = '\0';
r2[0] = '\0';
r3[0] = '\0';
r4[0] = '\0';
}
head=head->prev; // Head now points to last instruction
free(head->next);
head->next = NULL;
//at the end, head should point to the prev of last instruction.
//In which case it is the 'null' instr, move back one pointer and free this one.
// Initializing the dependancy arrays for each instruction
Instruction temp = head;
while(temp != NULL){
temp->numInstructions = numlines;
while(index < 5){
temp->depends[index] = malloc(sizeof(struct Restriction));
temp->depends[index]->restricts = NULL;
temp->depends[index]->isAnti = 0;
temp->depends[index]->bothTrueMem = 0;
temp->depends[index]->depMemDone = 0;
temp->depends[index]->depAntiDone = 0;
//temp->depends[index]->restricts = malloc(sizeof(struct Command));
//temp->depends[index]->restricts->isDone= 0;
index++;
}
temp->successors = (Instruction *) malloc(sizeof(Instruction)*numlines);
index = 0;
while(index < numlines){
temp->successors[index] = NULL;
//temp->successors[index] = malloc(sizeof(struct Command));
//temp->successors[index]->isDone = 0;
index++;
}
index = 0;
temp->memoryDepends = malloc(sizeof(Instruction)*numlines);
for(index = 0; index < numlines; index++)
temp->memoryDepends[index] = NULL;
index = 0;
temp = temp->prev;
}
//puts("here");
*node = head;
}
void chooseOp(Instruction head, char* token){
//Simple function to find the opcode responsible for this instruction
//printf("%s",token);
if(strcasecmp("nop", token) == 0){
head->opcode= NOP;
head->ismem=1;
head->latency = 1;
}
else if(strcasecmp("addI", token) == 0){
head->opcode= ADDI;
head->ismem=1;
head->latency = 1;
}
else if(strcasecmp("add", token) == 0){
head->opcode= ADD;
head->ismem=1;
head->latency = 1;
}
else if(strcasecmp("subI", token) == 0){
head->opcode= SUBI;
head->ismem=1;
head->latency = 1;
}
else if(strcasecmp("sub", token) == 0){
head->opcode= SUB;
head->ismem=1;
head->latency = 1;
}
else if(strcasecmp("mult", token) == 0){
head->opcode= MULT;
head->ismem=1;
head->latency = 3;
}
else if(strcasecmp("div", token) == 0){
head->opcode= DIV;
head->ismem=1;
head->latency = 3;
}
else if(strcasecmp("load", token) == 0){
head->opcode= LOAD;
head->ismem=0;
head->ismemread= 1;
head->latency = 5;
}
else if(strcasecmp("loadI", token) == 0){
head->opcode= LOADI;
head->ismem=0;
head->ismemread= 1;
head->latency = 1;
}
else if(strcasecmp("loadAO", token) == 0){
head->opcode= LOADAO;
head->ismem=0;
head->ismemread= 1;
head->latency = 5;
}
else if(strcasecmp("loadAI", token) == 0){
head->opcode= LOADAI;
head->ismem=0;
head->ismemread= 1;
head->latency = 5;
}
else if(strcasecmp("store", token) == 0){
head->opcode= STORE;
head->ismem=0;
head->ismemwrite = 1;
head->latency = 5;
}
else if(strcasecmp("storeAO", token) == 0){
head->opcode= STOREAO;
head->ismem=0;
head->ismemwrite= 1;
head->latency = 5;
}
else if(strcasecmp("storeAI", token) == 0){
head->opcode= STOREAI;
head->ismem=0;
head->ismemwrite= 1;
head->latency = 5;
}
else if(strcasecmp("output", token) == 0){
head->opcode= OUTPUT;
head->ismem=0; //Need to ask prof about this
head->ismemread= 1;
head->ismemwrite= 1;
head->latency = 1;
}
}
void createReg(Instruction head, char* token, int index){
Register reg = (Register) malloc(sizeof(struct Values));
if(token[0] != 'r' && token[0] != 'R'){
reg->isRegister = 1;
reg->value = atoi(token);
}
else{
reg->isRegister = 0;
reg->value = atoi(&token[1]);
}
switch (index) {
case 0:
head->firstReg = reg;
break;
case 1:
head->secondReg = reg;
break;
case 2:
head->outputReg1 = reg;
break;
case 3:
head->outputReg2 = reg;
break;
default:
break;
}
}
// Test to see if there is no file existing for stdin?
void printNode(Instruction head){
Register r1 = head->firstReg;
Register r2 = head->secondReg;
Register o1 = head->outputReg1;
Register o2 = head->outputReg2;
switch(head->opcode){
case NOP:
fprintf(stdout, "nop\n");
break;
case ADD:
fprintf(stdout, "add r%d, r%d => r%d\n", r1->value, r2->value, o2->value);
break;
case SUB:
fprintf(stdout, "sub r%d, r%d => r%d\n", r1->value, r2->value, o2->value);
break;
case MULT:
fprintf(stdout, "mult r%d, r%d => r%d\n", r1->value, r2->value, o2->value);
break;
case DIV:
fprintf(stdout, "div r%d, r%d => r%d\n", r1->value, r2->value, o2->value);
break;
case LOADAO:
fprintf(stdout, "loadAO r%d, r%d => r%d\n", r1->value, r2->value, o2->value);
break;
case STOREAO:
fprintf(stdout, "storeAO r%d => r%d, r%d\n", r1->value, o1->value, o2->value);
break;
case ADDI:
fprintf(stdout, "addI r%d, %d => r%d\n", r1->value, r2->value, o2->value);
break;
case SUBI:
fprintf(stdout, "subI r%d, %d => r%d\n", r1->value, r2->value, o2->value);
break;
case LOADAI:
fprintf(stdout, "loadAI r%d, %d => r%d\n", r1->value, r2->value, o2->value);
break;
case LOAD:
fprintf(stdout, "load r%d => r%d\n", r1->value, o1->value);
break;
case STORE:
fprintf(stdout, "store r%d => r%d\n", r1->value, o1->value);
break;
case LOADI:
fprintf(stdout, "loadI %d => r%d\n", r1->value, o1->value);
break;
case STOREAI:
fprintf(stdout, "storeAI r%d => r%d, %d\n", r1->value, o1->value, o2->value);
break;
case OUTPUT:
fprintf(stdout, "output %d\n", r1->value);
break;
default: //nop
fprintf(stdout, "nop\n");
}
}
void debugPrint(Instruction head){
while(head != NULL){
printNode(head);
head = head->prev;
}
}
void printAllDeps(Instruction head){
Instruction temp = head;
while(temp != NULL){
printDeps(temp);
temp = temp->prev;
}
}
void printDeps(Instruction head){
printNode(head);
int i = 0;
while(i < 5){
printf("Dependency %d is: \n", i);
if(head->depends[i]->restricts!=NULL){
printNode(head->depends[i]->restricts);
}
i++;
}
i = 0;
if(head->ismem==0){
while(i < head->numInstructions){
if(head->memoryDepends[i] != NULL){
printf("Memory Dependency %d is: \n", i);
printNode(head->memoryDepends[i]);
}
i++;
}
}
puts(" ");
}
void print(Instruction head){
//sort(head);
while(head != NULL){
printNode(head);
printf("head cycle is: %d\n", head->cycle);
head = head->next;
}
}