-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
312 lines (265 loc) · 8.05 KB
/
main.c
File metadata and controls
312 lines (265 loc) · 8.05 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
#include "header.h"
#include <pthread.h>
IntArray instructions;
LabelArray label_table;
extern struct KeyVal CompTable, JumpTable, DestTable;
extern int comp_count, jump_count, dest_count;
int next_variable_address = 16; // Start variable allocation at address 16
// Command line flags
int DEBUG_MODE = 0; // -d flag for debug output
int VERBOSE_MODE = 0; // -v flag for verbose output
int main(int argc, char *argv[])
{
char *input_file = NULL;
char *output_file = NULL;
// Parse command line arguments
for (int i = 1; i < argc; i++)
{
if (strcmp(argv[i], "-d") == 0)
{
DEBUG_MODE = 1;
}
else if (strcmp(argv[i], "-v") == 0)
{
VERBOSE_MODE = 1;
}
else if (argv[i][0] == '-')
{
fprintf(stderr, "Unknown option: %s\n", argv[i]);
fputs("Usage: ./prog [-d] [-v] <input.asm> <output.hack>\n", stderr);
fputs(" -d: Enable debug output for invalid instructions\n", stderr);
fputs(" -v: Enable verbose output showing all instructions\n", stderr);
return 1;
}
else if (input_file == NULL)
{
input_file = argv[i];
}
else if (output_file == NULL)
{
output_file = argv[i];
}
else
{
fputs("Too many arguments.\n", stderr);
fputs("Usage: ./prog [-d] [-v] <input.asm> <output.hack>\n", stderr);
return 1;
}
}
if (input_file == NULL || output_file == NULL)
{
fputs("Usage: ./prog [-d] [-v] <input.asm> <output.hack>\n", stderr);
fputs(" -d: Enable debug output for invalid instructions\n", stderr);
fputs(" -v: Enable verbose output showing all instructions\n", stderr);
return 1;
}
__init();
FILE *file = open_file(input_file);
__setup(file);
int output_size = instructions.size * 17;
char *buffer = malloc(output_size);
if (buffer == NULL)
{
fprintf(stderr, "Error: Failed to allocate output buffer of size %d\n", output_size);
fclose(file);
return 1;
}
printf("Compiling %d instructions, allocated %d bytes for output\n", instructions.size, output_size);
compile(file, buffer);
FILE *outfile = fopen(output_file, WRITE_FLAGS);
if (outfile == NULL)
{
perror("Failed to open output file");
free(buffer);
fclose(file);
return 1;
}
fputs(buffer, outfile);
fclose(outfile);
printf("Compiled Successfully!\n");
free(buffer);
fclose(file);
// Cleanup
free_int_array(&instructions);
free_label_array(&label_table);
return 0;
}
void compile(FILE *file, char *buffer)
{
rewind(file);
buffer[0] = '\0';
int buffer_pos = 0;
int buffer_size = instructions.size * 17 + 1;
int current_line = 0;
for (int i = 0; i < instructions.size; i++)
{
char line[MAX_LINE];
get_line_by_number_sequential(file, line, sizeof(line), instructions.data[i], ¤t_line);
strip_line_endings(line); // Remove \r and \n
char *line_no_whitespace = remove_whitespace(line);
// Remove inline comment from line_no_whitespace
char *comment_start = strstr(line_no_whitespace, "//");
if (comment_start != NULL)
{
*comment_start = '\0';
}
if (VERBOSE_MODE)
{
printf("Instruction %d (line %d): %s\n", i + 1, instructions.data[i] + 1, line_no_whitespace);
}
if (get_instruction_type(line_no_whitespace))
{
// c instruction: dest=comp;jump
// pick dest, or null when empty, max 3rd char is =
char *dest = malloc(4);
dest[0] = '\0'; // Initialize as empty string
int dest_in = 1;
int dest_found = 0;
while (dest_in < 4)
{
dest_in++;
if (line_no_whitespace[dest_in - 1] == '=')
{
dest_found = 1; // Set the existing variable, don't declare new one
break;
}
}
if (dest_found)
{
strncpy(dest, line_no_whitespace, dest_in - 1);
dest[dest_in - 1] = '\0'; // Null terminate
}
// pick comp, check up to ;, if no = all is comp
char *comp = malloc(4);
comp[0] = '\0'; // Initialize as empty string
int comp_start = dest_found ? dest_in : 0; // Start after '=' or from beginning
int comp_len = 0;
while (line_no_whitespace[comp_start] != '\0' && line_no_whitespace[comp_start] != ';' && comp_len < 3)
{
comp[comp_len] = line_no_whitespace[comp_start];
comp_len++;
comp_start++;
}
comp[comp_len] = '\0';
// pick jump after ;
char *jump = malloc(4); // Max jump length
jump[0] = '\0'; // Initialize as empty string
if (line_no_whitespace[comp_start] == ';')
{
strcpy(jump, line_no_whitespace + comp_start + 1); // Copy after ';'
}
// Generate C-instruction binary
int comp_value = get_Value(&CompTable, comp_count, comp);
int dest_value = get_Value(&DestTable, dest_count, dest);
int jump_value = get_Value(&JumpTable, jump_count, jump);
if (comp_value != -1 && dest_value != -1 && jump_value != -1)
{
char output[] = "1110000000000000";
// Set a-bit (bit 3) if comp uses M
if (strchr(comp, 'M') != NULL)
{
output[3] = '1';
}
int_to_bin(comp_value, output, 4, 9);
int_to_bin(dest_value, output, 10, 12);
int_to_bin(jump_value, output, 13, 15);
// Append to buffer
if (buffer_pos + 17 >= buffer_size)
{
fprintf(stderr, "Error: Buffer overflow at instruction %d\n", i + 1);
exit(EXIT_FAILURE);
}
strcpy(buffer + buffer_pos, output);
buffer_pos += INSTRUCTION_BITS; // Move position by 16 characters
strcpy(buffer + buffer_pos, "\n");
buffer_pos += 1; // Move position by 1 for newline
}
else
{
if (DEBUG_MODE || VERBOSE_MODE)
{
fprintf(stderr, "Debug: dest='%s', comp='%s', jump='%s'\n", dest, comp, jump);
}
fprintf(stderr, "Error: Invalid C-instruction at line %d\n'%s'\n", instructions.data[i] + 1, line_no_whitespace);
exit(EXIT_FAILURE);
}
free(dest);
free(comp);
free(jump);
}
else
{
// a instruction
char output[] = "0000000000000000";
if (check_if_A_numeric(line_no_whitespace))
{
int_to_bin(atoi(line_no_whitespace + 1), output, 1, 15);
}
else if (check_if_known_label(line_no_whitespace + 1))
{
int value = get_Value(label_table.data, label_table.size, line_no_whitespace + 1);
int_to_bin(value, output, 1, 15);
}
else
{
// Check if this is a variable that needs to be allocated
char *symbol = line_no_whitespace + 1;
int allocated_address = allocate_variable(symbol);
int_to_bin(allocated_address, output, 1, 15);
}
output[16] = '\0';
// Append to buffer
if (buffer_pos + 17 >= buffer_size)
{
fprintf(stderr, "Error: Buffer overflow at instruction %d\n", i + 1);
exit(EXIT_FAILURE);
}
strcpy(buffer + buffer_pos, output);
buffer_pos += INSTRUCTION_BITS;
strcpy(buffer + buffer_pos, "\n");
buffer_pos += 1;
}
free(line_no_whitespace);
}
buffer[buffer_pos] = '\0';
}
void __setup(FILE *file)
{
int ln = 0;
int pc = 0;
char line[MAX_LINE];
while (get_line(file, line, sizeof(line)))
{
strip_line_endings(line); // Remove \r and \n
char *line_no_whitespace = remove_whitespace(line);
if (check_if_comment(line_no_whitespace))
{
free(line_no_whitespace);
ln++;
continue;
}
if (check_if_label(line_no_whitespace))
{
char *target = malloc(MAX_LINE);
if (get_label(line_no_whitespace, target))
{
fprintf(stderr, "Error: Invalid label declaration'%s' at line %d\n", line_no_whitespace, ln + 1);
free(target);
free(line_no_whitespace);
exit(EXIT_FAILURE);
}
add_dynamic_label(&label_table, target, pc);
free(target);
free(line_no_whitespace);
ln++;
continue;
}
else
{
add_instruction(&instructions, ln);
}
pc++;
free(line_no_whitespace);
ln++;
}
}