-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsecondPass.c
More file actions
225 lines (200 loc) · 6.82 KB
/
Copy pathsecondPass.c
File metadata and controls
225 lines (200 loc) · 6.82 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
/* ------------------------------- includes ------------------------------- */
#include "secondPass.h"
#include "utils/errors.h"
#include "utils/vector.h"
#include "fileStructures/symbolTable.h"
#include "fileStructures/codeSeg.h"
#include "fileStructures/dataSeg.h"
#include "fileStructures/entryLines.h"
#include "fileStructures/externUsages.h"
/* ---------------------- helper function declaration ---------------------- */
void print_usages_errors (OpLinesList *op_list, char *file_name);
Symbol* is_unresolved_usages (Operand *operand);
void print_entry_errors (EntryLinesList *entry_list, char *file_name);
exit_code print_analyze (file_analyze *f);
void print_memory_img (OpLinesList *op_list, int ic, DataSegment *data_segment, int dc, FILE *stream);
/* ------------------------------------------------------------------------- */
exit_code secondPass (file_analyze *f)
{
exit_code res = ERROR;
int unresolved_entries = f->symbol_table->unresolved_entry_count;
int unresolved_usages = f->symbol_table->unresolved_symbols_count;
int len = (int) strlen (f->file_name);
strcat (f->file_name, ".am");
/* only unresolved symbols */
if (unresolved_entries == 0 && unresolved_usages > 0) {
print_usages_errors (f->op_list, f->file_name);
}
/* only unresolved entries */
if (unresolved_entries > 0 && unresolved_usages == 0) {
print_entry_errors (f->entry_list, f->file_name);
}
/* both unresolved entries and symbols */
if (unresolved_entries > 0 && unresolved_usages > 0) {
print_usages_errors (f->op_list, f->file_name);
print_entry_errors (f->entry_list, f->file_name);
}
/* no errors at all */
f->file_name[len] = '\0';
if (unresolved_entries == 0 && unresolved_usages == 0) {
if (f->error == 0) {
update_data_symbol_addresses (f->symbol_table, f->IC);
res = print_analyze (f);
}
}
#ifdef DEBUG_SECOND_PASS
if (res != MEMORY_ERROR){
display_debug(f, stdout, "first");
}
#endif
free_file_analyze (f);
return res;
}
/**
* @brief Prints errors related to unresolved symbol usages in operands.
*
* Traverses the operator list and identifies unresolved symbol usages in
* operands. Prints error messages for each unresolved symbol.
*
* @param op_list Pointer to the opcode list.
* @param file_name Name of the file being analyzed.
*/
void print_usages_errors (OpLinesList *op_list, char *file_name)
{
OpLine *op_line;
int i = 0;
Symbol *src_symbol = NULL, *target_symbol = NULL;
while ((op_line = get_op_line (op_list, i++))) {
/* unresolved symbol in src operand */
if ((src_symbol = is_unresolved_usages (&op_line->analyze->src))) {
raise_second_pass_err (op_line->parts, src_symbol->word, file_name);
}
/* unresolved symbol in target operand t*/
if ((target_symbol = is_unresolved_usages (&op_line->analyze->target))) {
/* print error only if target makes new error for this line */
if (target_symbol != src_symbol && !src_symbol) {
raise_second_pass_err (op_line->parts, target_symbol->word, file_name);
}
}
}
}
/**
* @brief Check if an operand using ab unresolved symbol
*
* Checks if an operand contains an unresolved symbol usage and returns
* a pointer to the unresolved symbol if found.
*
* @param operand Pointer to the operand to be checked.
* @return Pointer to the unresolved symbol if found, otherwise NULL.
*/
Symbol *is_unresolved_usages (Operand *operand)
{
Symbol *symbol;
Symbol_Data *symbol_data;
AddressingMode addr_mode = operand->add_mode;
if (addr_mode == DIRECT_ADD || addr_mode == INDEX_ADD) {
symbol = (Symbol *) operand->info.symInx.symbol;
symbol_data = (Symbol_Data *) symbol->data;
if (symbol_data->type == UNRESOLVED_ENTRY_USAGE
|| symbol_data->type == UNRESOLVED_USAGE) {
return symbol;
}
}
return NULL;
}
/**
* @brief Prints errors related to unresolved entry symbols.
*
* Traverses the entry list and identifies unresolved entry symbols.
* Prints error messages for each unresolved symbol.
*
* @param entry_list Pointer to the entry list.
* @param file_name Name of the file being analyzed.
*/
void print_entry_errors (EntryLinesList *entry_list, char *file_name)
{
EntryLine *entry_line;
Symbol *symbol;
Symbol_Data *symbol_data;
int i = 0;
while ((entry_line = get_entry_line (entry_list, i++))) {
symbol = entry_line->symbol;
symbol_data = (Symbol_Data *) symbol->data;
if (symbol_data->type == UNRESOLVED_ENTRY
|| symbol_data->type == UNRESOLVED_ENTRY_USAGE) {
raise_second_pass_err (entry_line->parts, entry_line->symbol->word, file_name);
}
}
}
/**
* @brief Prints output files of the assembler analyse
*
* Prints the memory image (file.ob), entry file (file.ent), and external
* file (file.ext)
*
* @param f Pointer to the file analysis structure.
* @return Exit code
*/
exit_code print_analyze (file_analyze *f)
{
FILE *ob_file, *ent_file, *ext_file;
exit_code res = SUCCESS;
/* --------- .ob file --------- */
ob_file = open_file (f->file_name, ".ob", "w");
if (!(ob_file)) {
return ERROR;
}
print_memory_img (f->op_list, f->IC, f->data_segment, f->DC, ob_file);
printf (" -- %s.ob created \n", f->file_name);
fclose (ob_file);
/* --------- .ent file --------- */
if (!is_empty (f->entry_list)) {
ent_file = open_file (f->file_name, ".ent", "w");
if (!(ent_file)) {
return ERROR;
}
print_entry_list (f->entry_list, ent_file);
printf (" -- %s.ent created \n", f->file_name);
fclose (ent_file);
}
/* --------- .ext file --------- */
if (f->symbol_table->extern_count > 0) {
ext_file = open_file (f->file_name, ".ext", "w");
if (!(ext_file)) {
return ERROR;
}
res = print_extern_table (f->op_list, ext_file, f->file_name);
if (res == SUCCESS) {
printf (" -- %s.ext created \n", f->file_name);
}
fclose (ext_file);
}
return res == MEMORY_ERROR ? MEMORY_ERROR : SUCCESS;
}
/**
* @brief Prints the memory image to the output stream (.ob file)
*
* Prints the memory image, including the code segment and data segment,
* to the specified output stream.
*
* @param op_list Pointer to the op list that will be the code segment
* @param ic Instruction count (IC).
* @param data_segment Pointer to the data segment.
* @param dc Data count (DC).
* @param stream Pointer to the output stream (file.ob)
*/
void print_memory_img (OpLinesList *op_list, int ic, DataSegment *data_segment,
int dc, FILE *stream)
{
fprintf (stream, "%4d %d\n", ic, dc);
if (ic > 0) {
print_code_segment (op_list, IC_START, (ic + IC_START), stream);
if (dc > 0) {
fputc ('\n', stream);
}
}
if (dc > 0) {
print_data_segment (data_segment, (IC_START + ic), (ic + dc + IC_START),
stream);
}
}