-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAuxiliaryFunctions .c
279 lines (237 loc) · 7.79 KB
/
AuxiliaryFunctions .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
#include "declarationsHeader.h"
extern struct keyWords keys[];
extern symPtr symTableHead;
/*
* The func takes as input a string and returns it without any white chars.
*/
void removeSpaces(char *sym) {
int cnt = 0, i = 0;
for (; sym[i]; i++) {
if (!isspace(sym[i])) {
sym[cnt++] = sym[i];
}
}
sym[cnt] = STR_END;
}
/*
* Returns the passed argument as a 14 long string in weird binary code.
*/
char *decToWeirdBinary(int num) {
int i;
unsigned int mask = 1;
char *res = (char *) malloc(sizeof(char) * (MEM_WORD_LEN));
MEMORY_ERROR(res)
res[MEM_WORD_LEN] = STR_END;/*Putting the null char.*/
/*Takes the num and makes from it a 15 length long binary string.*/
for (i = MEM_WORD_LEN - 1; i >= 0; --i, mask <<= 1) {
if ((unsigned) num & mask)
res[i] = SLASH;
else
res[i] = DOT;
}
return res;
}
/*
* Tests for logic errors withing the command.
*/
void checkForErrors(char *cmd, Word machWord) {
short i;
for (i = 0; i < ACTIONS_NUM; i++) {
if (strcmp(keys[i].symbol, cmd) == EQUAL)
break;
}
/*Looking for miss use of the methods - this block tests srcOp..*/
if (!keys[i].legitSrcMethodZro &&
machWord->srcOp == IMMEDIATE_METHOD && keys[i].hasSrc)
printErrorWithComment(src_meth0_illegal, cmd);
else if (!keys[i].legitSrcMethodOne && machWord->srcOp == DIRECT_METHOD)
printErrorWithComment(src_meth1_illegal, cmd);
else if (!keys[i].legitSrcMethodTwo && machWord->srcOp == JUMP_METHOD)
printErrorWithComment(src_meth2_illegal, cmd);
else if (!keys[i].legitSrcMethodThr && machWord->srcOp == DIRECT_REGISTER)
printErrorWithComment(src_meth3_illegal, cmd);
/*This block tests DestOp part of the command.*/
if (!keys[i].legitDstMethodZro && machWord->destOp == 0 && keys[i].hasDst)
printErrorWithComment(dst_meth0_illegal, cmd);
else if (!keys[i].legitDstMethodOne && machWord->destOp == DIRECT_METHOD)
printErrorWithComment(dst_meth1_illegal, cmd);
else if (!keys[i].legitDstMethodTwo && machWord->destOp == JUMP_METHOD)
printErrorWithComment(dst_meth2_illegal, cmd);
else if (!keys[i].legitDstMethodThr && machWord->destOp == DIRECT_REGISTER)
printErrorWithComment(dst_meth3_illegal, cmd);
}
/*
* Copies the command within the line to the pass argument.
*/
void getCommand(char *cmd) {
int i = 0;
char lineCpy[LINE_SIZE];
strcpy(lineCpy, line);
/*Searching and coping the cmd to the passed argument.*/
for (; i < ACTIONS_NUM; i++) {
if (strstr(lineCpy, keys[i].symbol)) {
strcpy(cmd, keys[i].symbol);
return;
}
}
/*Notifying to the caller there's no cmd in the line.*/
cmd[0] = STR_END;
}
/*
* Returns the number of parameters within the line.
*/
size_t getNumberOfParametersInLine() {
const size_t ONE_PARAMETER = 1, TWO_PARAMETERS = 2;
char lineCpy[LINE_SIZE];
strcpy(lineCpy, line);
/*Returns number of parameters in the line, returns only 1 or 2.*/
if (strchr(lineCpy, COMMA) != NULL)
return TWO_PARAMETERS;
return strchr(lineCpy, COMMA) == NULL ? ONE_PARAMETER : TWO_PARAMETERS;
}
/*
* Returns the type of the operand.
*/
size_t findOperandType() {
char lineCpy[LINE_SIZE], *temp;
short i = 0, firstRegister = 20, cmdLength = 3;
symPtr ptr;
strcpy(lineCpy, line);
temp = (char *) malloc(sizeof(char) * LINE_SIZE);
/*These cmds doesnt have operands*/
if (strstr(lineCpy, "stop") != NULL || strstr(lineCpy, "rts") != NULL) {
free(temp);
return 0;
}
/*Getting to the line from the start of the cmd.*/
for (i = 0; i < ACTIONS_NUM; i++) {
if ((temp = strstr(lineCpy, keys[i].symbol)) != NULL)
break;
}
/*Getting pass the cmd to the operands.*/
temp += cmdLength;
/*Getting rid of white chars.*/
removeSpaces(temp);
/*If label and parenthesis its jump*/
if (temp[0] != O_BRACKETS && strchr(temp, O_BRACKETS) != NULL)
return JUMP_METHOD;
/*If no parameters and str == register its 3*/
if (strchr(temp, O_BRACKETS) == NULL)
for (i = firstRegister; i < NUM_OF_KEYS; i++)
if (strcmp(temp, keys[i].symbol) == EQUAL)
return DIRECT_REGISTER;
/* Else it has to be a label*/
for (ptr = symTableHead; ptr; ptr = ptr->next)
if (strcmp(temp, ptr->symName) == EQUAL)
return DIRECT_METHOD;
/*If we got here it means the parameter is neither of the above.*/
printErrorWithComment(undef_parameter, temp);
return 0;
}
/*
* Returns true if the line is a comment line, else returns false.
*/
boolean lineIsComment(char *line) {
char lineCopy[STRING_SIZE];
size_t i = 0;
strcpy(lineCopy, line);
/*Searching for ';' char within the line.*/
if (strchr(lineCopy, ISCOMMENT))
return TRUE;
for (i = 0; lineCopy[i]; i++) {
if (!isspace(lineCopy[i]))
break;
}
/*Blank line.*/
if (i == strlen(lineCopy))
return TRUE;
return FALSE;
}
void createLineList() {
int offset = 0;
const int ok = 1;
char str[LINE_SIZE];
char *lineCopy = (char *) malloc(sizeof(char) * LINE_SIZE);
MEMORY_ERROR(lineCopy)
/*Zeroing the size and working on a copy of the line*/
lineListLength = 0;
strcpy(lineCopy, line);
/*Extracting each string from the line*/
while (sscanf(lineCopy, "%s%n", str, &offset) == ok) {
lineCopy += offset;
addToLineList(str);
}
}
/*
* Returns True if the command is a binary command, else, returns false.
*/
boolean isBinaryAction(char *cmd) {
/*The following cmd's are binary commands.*/
if (strcmp(cmd, "mov") == EQUAL || strcmp(cmd, "cmp") == EQUAL)
return TRUE;
if (strcmp(cmd, "add") == EQUAL || strcmp(cmd, "sub") == EQUAL)
return TRUE;
if (strcmp(cmd, "lea") == EQUAL)
return TRUE;
return FALSE;
}
/*
* Takes a string as argument and fills it with the appropriate weird binary code.
*/
void
memWordToString(Word word, size_t intNum, size_t reg1, size_t reg2, char *tempWord, char *str) {
unsigned int *machWord = (unsigned int *) word, mask;
const size_t bothRegister = 1, maskForReg = 32, maskForAddAndCmd = 8192, twoReg = 2, last = 14;
/*32 = 0..0100000, 8192 = 0..010000000000000 in binary.*/
size_t i, j = 0;
if (generalError)
return;
if (intNum == bothRegister) {
for (i = 0; i < twoReg; i++) {
for (mask = maskForReg; mask; mask >>= 1, ++j) {
if ((unsigned) reg1 & mask) {
tempWord[j] = SLASH;
} else {
tempWord[j] = DOT;
}
}
/*Moving to the second register.*/
reg1 = reg2;
}
/*the ERA is absolute for register.*/
tempWord[j] = DOT;
tempWord[++j] = DOT;
} else {
/*Finds the binary form of the word and puts it into the buffer.*/
for (mask = maskForAddAndCmd; mask; mask >>= 1, ++j) {
if ((*(machWord + intNum)) & mask) {
tempWord[j] = SLASH;
} else {
tempWord[j] = DOT;
}
}
}
/*Making sure there's the null char is the string.*/
tempWord[last] = STR_END;
}
/*
* Returns true if the operand is with immediate method use.
* Else, returns false.
*/
boolean isImmediate(char *param) {
char paramCpy[LINE_SIZE];
strcpy(paramCpy, param);
/*Searching for '#' in the line.*/
if (strchr(paramCpy, IMMEDIATE_METHOD_ID))
return TRUE;
return FALSE;
}
char *getLineFromFile(char *line, FILE *fd) {
return fgets(line, LINE_SIZE, fd);
}
void parseLine(char *line) {
*strchr(line, '\n') = STR_END;
}
void incrementLinesNumber() {
++lineNumber;
}