-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSecondPass.c
196 lines (159 loc) · 4.61 KB
/
SecondPass.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
#include "declarationsHeader.h"
extern paramsPtr paramsListHead;
extern symPtr symTableHead;
extern insPtr insListHead;
extPtr extListHead;
extern struct keyWords keys[];
/*
* Takes one line and codes it.
*/
void secondPass(FILE *fd) {
if (generalError)
return;
/*Working with a ptr to the current line.*/
line = (char *) malloc(sizeof(char) * LINE_SIZE);
MEMORY_ERROR(line)
resetLineNumber();
while (getLineFromFile(line, fd)) {/*Taking one line at a time and coding it*/
parseLine(line);
incrementLinesNumber();
if (lineIsComment(line))
continue;
if (lineWithCommandDefinition()) {
codeCommand();
codeParameters();
} else if (lineWithEntryLabel())
preWritingToFileEnt();
}
/*Finds if an extern symbol was not used during the run.*/
externSymWasntUsed(fd);
if (!errorsInSymTable() && !generalError)
createFiles();
free(line);
}
boolean specialTypeAction(char *cmd) {
return strcmp(cmd, "jmp") == EQUAL || strcmp(cmd, "bne") == EQUAL ||
strcmp(cmd, "jsr") == EQUAL;
}
void findOpCode(Word machWord, char *cmd) {
int i;
for (i = 0; i < ACTIONS_NUM; i++)
if (strcmp(cmd, keys[i].symbol) == 0)
machWord->opCode = keys[i].numValue;
}
boolean nothingToCode(paramsPtr ptr, char *cmd) {
return ptr == NULL && strcmp(cmd, "stop") != EQUAL && strcmp(cmd, "rts") != EQUAL;
}
/*
*Sync's the parameters from the params list to the current line.
*/
void syncParametersToLine(paramsPtr *ptr) {
while ((*ptr) != NULL) {
if ((*ptr)->lineNum == lineNumber)
return;
(*ptr) = (*ptr)->next;
}
}
/*
* Tests if extern symbols weren't used over the file.
*/
void externSymWasntUsed(FILE *fd) {
char tempLine[LINE_SIZE], *tempSym;
extPtr ptr = extListHead;
boolean isFound = FALSE;
const size_t extLength = 7;
size_t lineNum = 0;
rewind(fd);
while (getLineFromFile(tempLine, fd)) {
++lineNum;
if (lineIsComment(tempLine))
continue;
isFound = FALSE;
if ((tempSym = strstr(tempLine, GUIDING_SENTENCE_EXTERN)) != NULL) {
tempSym += extLength;
removeSpaces(tempSym);
removeSpaces(tempLine);
for (ptr = extListHead; ptr && !isFound; ptr = ptr->next) {
if (strcmp(ptr->symbol, tempSym) == EQUAL)
isFound = TRUE;
}
if (ptr == NULL && !isFound)
printWarning(uns_ex_sym, tempSym, lineNum);
}
}
}
/*
* Tests if there are errors in a regular action code.
*/
void checkForErrorsInRegAction() {
char lineCpy[LINE_SIZE], *beforeParentheses;
size_t i = 0;
symPtr ptr;
strcpy(lineCpy, line);
if (strchr(lineCpy, O_BRACKETS) == NULL)
return;
beforeParentheses = strtok(lineCpy, O_BRACKETS_STR);
for (i = 0; i < ACTIONS_NUM; i++)
if (strstr(lineCpy, keys[i].symbol) != NULL) {
beforeParentheses = strstr(beforeParentheses, keys[i].symbol);
break;
}
for (ptr = symTableHead; ptr; ptr = ptr->next) {
if (strstr(beforeParentheses, ptr->symName)) {
printErrorWithComment(illegal_sym_mth, ptr->symName);
}
}
}
/*
*Returns true if the operand is with the 1st method use.
* Else, returns false.
*/
boolean paramIsLabel(char *param) {
symPtr ptr = symTableHead;
/*Testing if the param is a symbol*/
while (ptr != NULL) {
if (strcmp(ptr->symName, param) == EQUAL)
return TRUE;
ptr = ptr->next;
}
return FALSE;
}
/*
* Returns true if the operand is with the 4th method use.
* Else, returns false.
*/
boolean isRegister(char *param) {
int i, firstReg = 20;
/*Testing if the parameter is a register*/
for (i = firstReg; i < NUM_OF_KEYS; i++)
if (strcmp(keys[i].symbol, param) == EQUAL)
return TRUE;
return FALSE;
}
/*
* Returns true if there's an entry label within the line.
* Else, returns false.
*/
boolean lineWithEntryLabel() {
char lineCpy[LINE_SIZE];
strcpy(lineCpy, line);
/*Searching for .entry within the line.*/
if (strstr(lineCpy, GUIDING_SENTENCE_ENT))
return TRUE;
return FALSE;
}
/*
* Returns true if there's an extern label within the line.
* Else, returns false.
*/
boolean lineWithExternLabel() {
char lineCpy[LINE_SIZE];
strcpy(lineCpy, line);
/*Searching for .extern within the line.*/
if (strstr(lineCpy, GUIDING_SENTENCE_EXTERN))
return TRUE;
return FALSE;
}
void resetLineNumber() {
lineNumber = 0;
}