-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodeCollection.c
590 lines (525 loc) · 18.4 KB
/
codeCollection.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
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
#include "codeCollection.h"
#include "dataCollection.h"
#include "binaryConvertor.h"
#include "utils.h"
/* get this from the sharedCollections file*/
extern Action ValidActions[];
/* ic counter */
static int ic = 0;
/* this member will save the last value after the first iteration */
static int firstIterationLastIC = 0;
/* code collection */
static Word codeCollection[COLLECTION_SIZE];
/* an empty word to reset the array with */
static const Word EmptyWord;
int calculateActionIcPointer(char *action,char *actionAttr)
{
Action *selectedAction;
char firstOper[LINE_LENGTH] = "", secondOper[LINE_LENGTH] = "", extraData[LINE_LENGTH] = "";
int srcAddressingType = 0, destAddressingType = 0, icPointer = INVALID;
/*find action*/
selectedAction = getActionByName(action);
if(selectedAction != NULL)
{
/*handle action with 2 operands*/
if(selectedAction -> numOfOperands == 2)
{
/* get the operands */
if(sscanf(actionAttr," %[^ \r\t:,] , %[^ \r\t:,] %[^\n] ",firstOper,secondOper,extraData) >= 2)
{
if(strlen(extraData) != 0)
{
printErr("'%s' can not get more than 2 operands\n",action);
}
else
{
srcAddressingType = getOperandType(firstOper);
destAddressingType = getOperandType(secondOper);
/* check if the opearnads are valid for this action */
if(isValidAddressTypeForAction(srcAddressingType,selectedAction -> sourceOper) == true
&& isValidAddressTypeForAction(destAddressingType,selectedAction -> destOper) == true)
{
/* add the action word to the colleciton */
icPointer = addActionWord(selectedAction -> binaryIndex, selectedAction -> numOfOperands,
srcAddressingType,destAddressingType);
/* check how muc space is needed for the data word of this action */
getActionRefrenceinMemory(srcAddressingType, destAddressingType);
}
else
{
printErr("invalid addressing method for action '%s'\n",action);
}
}
}
else
{
printErr("%s expect to get 2 operands\n",action);
}
}
else if(selectedAction -> numOfOperands == 1)
{
/*handle action with 1 operands*/
destAddressingType = getOperandType(actionAttr);
if(isValidAddressTypeForAction(destAddressingType, selectedAction -> destOper) == false)
{
printErr("invalid addressing method for action '%s' \n",action);
}
else
{
/* add the action word to the colleciton */
icPointer = addActionWord(selectedAction -> binaryIndex, selectedAction -> numOfOperands,
0,destAddressingType);
/* check how much space is needed for the data word of this action */
getActionRefrenceinMemory(notUsedOper, destAddressingType);
}
}
else
{
/*handle actions that have zero operands*/
if(actionAttr != NULL && sscanf(actionAttr," %[^\n] ",extraData) == 1)
{
printErr("'%s' can not accept operands\n",action);
}
else
{
/* add action word */
icPointer = addActionWord(selectedAction -> binaryIndex, selectedAction -> numOfOperands,0,0);
/* check how much space is needed for the data word of this action */
getActionRefrenceinMemory(notUsedOper, notUsedOper);
}
}
}
else
{
printErr("'%s' is an invalid action name\n",action);
}
return icPointer;
}
int addActionWord(int actionIndex, int numOfOperands,int srcAddressingType,int destAddressingType)
{
int icPointer = INVALID;
WordDef * wordDef;
wordDef = createCommandWord(actionIndex, numOfOperands,
srcAddressingType,destAddressingType,absolute);
if(wordDef)
{
icPointer = addToCollection(wordDef,commandType);
}
return icPointer;
}
void addActionToCodeCollection(char *action,char *actionAttr){
Action *selectedAction;
char firstOper[LINE_LENGTH] = "", secondOper[LINE_LENGTH] = "", extraData[LINE_LENGTH] = "";
int srcAddressingType, destAddressingType = -1;
WordDef *wordDef;
/*find action*/
selectedAction = getActionByName(action);
if(selectedAction != NULL)
{
/*handle action with 2 operands*/
if(selectedAction -> numOfOperands == 2)
{
/* get the operands */
if(sscanf(actionAttr," %[^ \r\t:,] , %[^ \r\t:,] %[^\n] ",firstOper,secondOper,extraData) >= 2)
{
if(strlen(extraData) == 0)
{
srcAddressingType = getOperandType(firstOper);
destAddressingType = getOperandType(secondOper);
/* check if both operands are valid fot his action type */
if(isValidAddressTypeForAction(srcAddressingType,selectedAction -> sourceOper) == true
&& isValidAddressTypeForAction(destAddressingType,selectedAction -> destOper) == true)
{
if(srcAddressingType != directRegister)
addDataWordToCollection(srcAddressingType, firstOper);
/* if one of the operands is a register type add it to the collection */
if((srcAddressingType == directRegister && destAddressingType == directRegister) ||
(srcAddressingType == directRegister || destAddressingType == directRegister))
{
wordDef = createRegisterWord(srcAddressingType,firstOper, destAddressingType, secondOper);
addToCollection(wordDef, registerValueType);
}
if(destAddressingType != directRegister)
addDataWordToCollection(destAddressingType, secondOper);
}
}
}
}
else if(selectedAction -> numOfOperands == 1)
{
/*handle action with 1 operands*/
destAddressingType = getOperandType(actionAttr);
if(isValidAddressTypeForAction(destAddressingType,selectedAction -> destOper) == true)
{
/* check the operand type and add it to the collection */
if(destAddressingType == directRegister)
{
wordDef = createRegisterWord(notUsedOper, NULL, destAddressingType, actionAttr);
addToCollection(wordDef, registerValueType);
}
else
{
addDataWordToCollection(destAddressingType, actionAttr);
}
}
}
}
}
int addToCollection(WordDef * wordDef, int wordType)
{
int currentIc;
/*find the first slot open*/
while(codeCollection[ic].word)
{
ic++;
}
currentIc = ic;
/* add the word to this space */
codeCollection[ic].word = wordDef;
codeCollection[ic].wordType = wordType;
ic++;
/* return the ic pointer for this word */
return currentIc + IC_START_POSITION;
}
WordDef* createCommandWord(int opCode, int group, int src, int dest, int Era)
{
WordDef* wordDef = (WordDef*)malloc(sizeof(WordDef));
if(wordDef != NULL)
{
wordDef -> command.notUsed = NOT_USED_DEFAULT_VALUE;
wordDef -> command.opCode = opCode;
wordDef -> command.group = group;
wordDef -> command.dest = dest;
wordDef -> command.src = src;
wordDef -> command.ERA = Era;
return wordDef;
}
printf("can not allocate space for a command Word\n");
return NULL;
}
void addDataWordToCollection(int addressingType, char* value)
{
WordDef * wordDef;
int addedAtRow = -1, dataType = -1;
char *lineNumber;
/* find the right word type and create a word object */
if(addressingType == instant)
{
wordDef = createInstantWord(value);
dataType = regularValueType;
}
else if(addressingType == direct)
{
wordDef = createDirectWord(value);
dataType = regularValueType;
}
else if(addressingType == dynamic)
{
wordDef = createDynamicWord(value);
dataType = regularValueType;
}
if(wordDef)
{
/* add word to the code collection */
addedAtRow = addToCollection(wordDef, dataType);
if(wordDef -> regularValue.ERA == externalData)
{
/* write in external file */
lineNumber = getSpecialBase8String(addedAtRow);
externalWriteToFile("%s %s\n",value,lineNumber);
free(lineNumber);
}
}
}
WordDef* createDynamicWord(char* value)
{
/* data pointers */
WordDef * word;
Symbol * symbol;
char symbolName[LINE_LENGTH] = "";
int fromBit = -1, toBit = -1, refrence = -1, data = 0;
/* get the label and the two numbers specifing the range */
if(sscanf(value,"%[^[][%d-%d]",symbolName,&fromBit,&toBit) == 3
&& fromBit < toBit && fromBit >= 0 && toBit <= 13)
{
/* get the symbol by his name */
symbol = getSymbolByName(symbolName);
if(!symbol)
{
printErr("%s is never defined.\n",value);
}
else
{
word = (WordDef*)malloc(sizeof(WordDef));
if(word)
{
refrence = symbol -> icRefrence;
/* if instruction get the data from the data collection */
if(symbol -> commandType == instructionCommand)
{
refrence = symbol -> dcRefrence;
data = getDataByID(refrence);
}
else if(symbol -> commandType == actionCommand)
{
/* if command get the command ic position */
data = getCommandICPositionByRefrenceID(refrence);
}
/* get the new value for the specified range */
word -> regularValue.value = cutByBits(data, fromBit, toBit);
word -> regularValue.ERA = absolute;
}
else
{
printf("can't allocate space for new word \n");
}
}
}
else
{
printErr("%s is an invalid operandor\n",value);
}
return word;
}
/* get ic refrence and return the number that represents the command word */
unsigned int getCommandICPositionByRefrenceID(int refrenceID)
{
unsigned int result = 0;
Word * word;
WordDef * wordDef;
word = &codeCollection[refrenceID - IC_START_POSITION];
if(!word)
printf("there is no word in this index: %d\n",refrenceID);
else
{
wordDef = word -> word;
if(wordDef)
{
/*shift bits to get the number they represent*/
result = convertCommandWordToInt(wordDef);
}
}
return result;
}
WordDef* createDirectWord(char* value){
WordDef * word;
Symbol * symbol;
symbol = getSymbolByName(value);
if(symbol != NULL)
{
word = (WordDef*)malloc(sizeof(WordDef));
if(word != NULL)
{
word -> regularValue.value = symbol -> icRefrence;
word -> regularValue.ERA = relocatable;
if((symbol -> isExternal) == true)
{
word -> regularValue.ERA = externalData;
}
}
else
{
printf("can't allocate space for new word \n");
}
}
else
{
printErr("'%s' is never defined.\n",value);
}
return word;
}
/* get data word */
WordDef* createInstantWord(char* value)
{
WordDef * word;
int number = 0;
char sign = '+';
word = (WordDef*)malloc(sizeof(WordDef));
if(word != NULL)
{
word -> regularValue.ERA = absolute;
if(sscanf(value,"#%[-+]%d",&sign,&number) == 2 || sscanf(value,"#%d",&number) == 1)
{
if(sign == '-')
{
number *= -1;
}
word -> regularValue.value = number;
}
}
else
{
printErr("can't allocate space for new instant word \n");
}
return word;
}
WordDef* createRegisterWord(int srcAddressType, char* srcWord, int destAdressType, char* destWord)
{
WordDef * word;
int srcNumber = 0, destNumber = 0;
word = (WordDef*)malloc(sizeof(WordDef));
if(word != NULL)
{
word -> registerValue.notUsed = 0;
word -> registerValue.ERA = absolute;
word -> registerValue.dest = 0;
word -> registerValue.src = 0;
if(destAdressType == directRegister && sscanf(destWord,"r%d",&destNumber) == 1)
{
word -> registerValue.dest = destNumber;
}
if(srcAddressType == directRegister && sscanf(srcWord,"r%d",&srcNumber) == 1)
{
word -> registerValue.src = srcNumber;
}
}
else
{
printErr("can't allocate space for new word \n");
}
return word;
}
Action* getActionByName(const char* name)
{
int i = 0;
for(i = 0; i < 16; i++)
{
if(strcmp((ValidActions[i].name),name) == 0)
{
return &ValidActions[i];
}
}
return NULL;
}
int getOperandType(const char* oper)
{
int number = -1, number2 = -1, operandType = invalidOperand;
/* char arrays for sscanf */
char operAttr[LINE_LENGTH] = "", extraDataInstent[LINE_LENGTH] = "",
extraDataDirect[LINE_LENGTH] = "", extraDataDynamic[LINE_LENGTH] = "", extraDataRegister[LINE_LENGTH] = "";
if(sscanf(oper," #%*[-+]%d %[^\n]",&number,extraDataInstent) >= 1 ||
sscanf(oper," #%d %[^\n]",&number,extraDataInstent) >= 1)
{
/* check if the instant operans is valid */
if(strlen(extraDataInstent) == 0 && number != -1)
operandType = instant;
}
else if(sscanf(oper," %*[r]%d %[^\n]",&number,extraDataRegister) >= 1 && strlen(extraDataRegister) == 0)
{
/* check if the register operand is valid */
if(number >= 0 && number <= 7)
operandType = directRegister;
else
operandType = direct;
}
else if(sscanf(oper," %*[^[][%d-%d] %[^\n]",&number,&number2,extraDataDynamic) >= 2 ||
sscanf(oper," %*[^[][%d-] %[^\n]",&number,extraDataDynamic) >= 1 ||
sscanf(oper," %*[^[][-%d] %[^\n]",&number,extraDataDynamic) >= 1)
{
/* check if the dynamic operand is valid */
if(strlen(extraDataDynamic) == 0 && number < number2 && number >= 0 && number2 <= 13)
operandType = dynamic;
}
else if(sscanf(oper," %[^ \t\r] %[^\n]",operAttr,extraDataDirect) >= 1 && strchr(oper,'[') == NULL
&& strchr(oper,']') == NULL && strlen(extraDataDirect) == 0)
{
operandType = direct;
}
return operandType;
}
int isValidAddressTypeForAction(int sourcingType, int* validAddressingTypes)
{
int i = 0, arrayMaxLength = 4;
for(i = 0; i < arrayMaxLength; i++)
{
if(validAddressingTypes[i] == -1)
return false;
if(validAddressingTypes[i] == sourcingType)
return true;
}
return false;
}
int getActionRefrenceinMemory(int srcAddressingType, int destAddressingType)
{
int numberOfRows = 0, currentIc = ic;
if(destAddressingType == directRegister && srcAddressingType == directRegister)
{
numberOfRows += 1;
}
else if(srcAddressingType != notUsedOper && destAddressingType != notUsedOper)
{
numberOfRows += 2;
}
else if((srcAddressingType == notUsedOper && destAddressingType != notUsedOper) ||
(destAddressingType == notUsedOper && srcAddressingType != notUsedOper))
{
numberOfRows += 1;
}
ic += numberOfRows;
return currentIc + IC_START_POSITION;
}
int getICPointer()
{
return ic + IC_START_POSITION;
}
int getICCollectionSize()
{
return ic;
}
void resetIc()
{
ic = 0;
}
void saveLastICPointer()
{
firstIterationLastIC = ic;
}
void cleanLastICPointer()
{
firstIterationLastIC = 0;
}
void printCodeCollection(){
Word *word;
WordDef * base8;
int i = 0, wordAsInt = 0;
char *output = "", *addressOutput = "";
for(i = 0; i < firstIterationLastIC; i++)
{
word = &codeCollection[i];
if(word != NULL && word -> word != NULL)
{
if(word -> wordType == commandType)
{
/* objWriteToFile("%d|%d|%d|%d|%d|%d\n",word -> word -> command.notUsed,word -> word -> command.group,word -> word -> command.opCode,
word -> word -> command.src,word -> word -> command.dest,word -> word -> command.ERA);*/
wordAsInt = convertCommandWordToInt(word -> word);
}
else if(word -> wordType == registerValueType)
{
wordAsInt = convertRegisterValueWordToInt(word -> word);
/*objWriteToFile(" %d | %d | %d | %d \n",word -> word -> registerValue.notUsed, word -> word -> registerValue.src,
word -> word -> registerValue.dest,word -> word -> registerValue.ERA);*/
}
else if(word -> wordType == regularValueType)
{
wordAsInt = convertRegularValueWordToInt(word -> word);
/*objWriteToFile(" %d | %d \n",word -> word -> regularValue.value, word -> word -> regularValue.ERA);*/
}
base8 = convertDecimalNumberToBase8Word(wordAsInt);
output = getStringFromBase8Word(base8);
addressOutput = getSpecialBase8String(i+IC_START_POSITION);
objWriteToFile("%s %s\n",addressOutput, output);
free(base8);
free(output);
free(addressOutput);
}
}
}
void cleanCodeCollection()
{
int i = 0;
for(i = 0;i < COLLECTION_SIZE; i++)
{
codeCollection[i] = EmptyWord;
}
}