This repository has been archived by the owner on Dec 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
422 lines (362 loc) · 11.2 KB
/
main.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
/*
* main.c - Accepts files for the 120++ compiler.
*
* Copyright (C) 2014 Andrew Schwartzmeyer
*
* This file released under the AGPLv3 license.
*/
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <argp.h>
#include <libgen.h>
#include <time.h>
#include "args.h"
#include "logger.h"
#include "libs.h"
#include "lexer.h"
#include "symbol.h"
#include "node.h"
#include "scope.h"
#include "intermediate.h"
#include "final.h"
#include "list.h"
#include "tree.h"
#include "hasht.h"
/* argument parser */
const char *argp_program_version = "120++ hw5";
const char *argp_program_bug_address = "<andrew@schwartzmeyer.com>";
static char doc[] = "Andrew Schwartzmeyer's 120++ compiler.\v"
"University of Idaho - Department of Computer Science\n"
"CS 445: Compiler and Translator Design, by Dr. Clinton Jeffery"
"\n\n"
"This is a complete old-school compiler using Flex and Bison for a "
"subset of C++ and C, dubbed '120++' as it should work for most code "
"written in our CS 120 course, including basic classes. Notable "
"exceptions include exceptions, templates, virtual semantics, "
"namespaces, variadics, casting, conversions, typedefs, access and "
"storage qualifiers, operator overloading, and assembly code "
"generation. The final output is three-address C code, compilable by "
"GCC, which demonstrates the correctness of the lexer, parser, "
"semantic analysis, memory layout, and intermediate code generation. "
"See the 'data/pass' folder for examples of valid code, and run "
"`make smoke` to see their output."
"\n\n"
"This repo is located @ https://github.com/andschwa/uidaho-cs445"
"\n\n"
"This program is licensed under the AGPLv3, see the LICENSE file.\n";
static char args_doc[] = "infile...";
static struct argp_option options[] = {
{ "debug", 'd', 0, 0, "Print debug messages (scopes, mostly).\n"
"Also disables exit on assertion failure."},
{ "tree", 't', 0, 0, "Print the syntax tree." },
{ "syntax", 't', 0, OPTION_ALIAS },
{ "symbols", 'y', 0, 0, "Print the populated symbols." },
{ "checks", 'k', 0, 0, "Print the performed type checks." },
{ "types", 'k', 0, OPTION_ALIAS },
{ "include", 'I', "DIR", 0, "Search path for 'system' headers." },
{ "assemble", 's', 0, 0, "Generate assembler code." },
{ "compile", 'c', 0, 0, "Generate object code." },
{ "output", 'o', "FILE", 0, "Name of generated executable." },
{ 0 }
};
static error_t parse_opt(int key, char *arg, struct argp_state *state);
static struct argp argp = { options, parse_opt, args_doc, doc };
/* shared with lexer and parser */
struct tree *yyprogram;
struct list *yyscopes;
struct list *yyfiles;
struct list *yyclibs;
struct hasht *yyincludes;
struct hasht *yytypes;
size_t yylabels;
enum region region;
size_t offset;
static void parse_program(char *filename);
/* from lexer */
void free_typename(struct hasht_node *t);
/* from parser */
int yyparse();
bool print_tree(struct tree *t, int d);
int main(int argc, char **argv)
{
arguments.debug = false;
arguments.tree = false;
arguments.symbols = false;
arguments.checks = false;
arguments.assemble = false;
arguments.compile = false;
arguments.output = "a.out";
arguments.include = getcwd(NULL, 0);
argp_parse(&argp, argc, argv, 0, 0, &arguments);
char *objects = "";
/* parse each input file as a new 'program' */
for (int i = 0; arguments.input_files[i]; ++i) {
char *filename = realpath(arguments.input_files[i], NULL);
if (filename == NULL)
log_error("could not find input file: %s",
arguments.input_files[i]);
/* copy because Wormulon's basename modifies */
char *copy = strdup(filename);
const char *base = basename(copy);
free(copy);
/* append object name to end of objects */
char *temp;
asprintf(&temp, "%s %s.o", objects, base);
/* free if previously allocated */
if (strcmp(objects, "") != 0)
free(objects);
objects = temp;
parse_program(filename);
}
/* link object files */
if (!arguments.assemble && !arguments.compile) {
char *command;
asprintf(&command, "gcc -o %s%s", arguments.output, objects);
int status = system(command);
if (status != 0)
log_error("command failed: %s", command);
free(command);
/* remove object files */
asprintf(&command, "rm -f%s", objects);
status = system(command);
if (status != 0)
log_error("command failed: %s", command);
free(command);
}
return EXIT_SUCCESS;
}
void parse_program(char *filename)
{
printf("parsing file: %s\n", filename);
yyfiles = list_new(NULL, &free);
log_assert(yyfiles);
list_push_back(yyfiles, filename);
yyclibs = list_new(NULL, &free);
log_assert(yyclibs);
yyincludes = hasht_new(8, true, NULL, NULL, NULL);
log_assert(yyincludes);
/* open file for lexer */
yyin = fopen(filename, "r");
if (yyin == NULL)
log_error("could not open input file: %s", filename);
/* push buffer state for lexer */
yypush_buffer_state(yy_create_buffer(yyin, YY_BUF_SIZE));
/* setup yytypes table for lexer */
yytypes = hasht_new(8, true, NULL, NULL, &free_typename);
log_assert(yytypes);
/* reset library flags */
libs.usingstd = false;
libs.cstdlib = false;
libs.cmath = false;
libs.ctime = false;
libs.cstring = false;
libs.fstream = false;
libs.iostream = false;
libs.string = false;
libs.iomanip = false;
log_debug("invoking Bison");
int result = yyparse();
if (result != 0)
exit(2);
/* print syntax tree */
if (arguments.tree)
tree_traverse(yyprogram, 0, &print_tree, NULL, NULL);
/* initialize scope stack */
log_debug("setting up for semantic analysis");
yyscopes = list_new(NULL, NULL);
log_assert(yyscopes);
struct hasht *global = hasht_new(32, true, NULL, NULL, &symbol_free);
log_assert(global);
list_push_back(yyscopes, global);
/* build the symbol tables */
log_debug("populating symbol tables");
region = GLOBE_R;
offset = 0;
symbol_populate(yyprogram);
log_debug("global scope had %zu symbols", hasht_used(global));
/* constant symbol table put in front of stack for known location */
struct hasht *constant = hasht_new(32, true, NULL, NULL, &symbol_free);
log_assert(constant);
list_push_front(yyscopes, constant);
region = CONST_R;
offset = 0;
log_debug("type checking");
type_check(yyprogram);
/* generating intermediate code */
log_debug("generating intermediate code");
yylabels = 0; /* reset label counter */
code_generate(yyprogram);
struct list *code = ((struct node *)yyprogram->data)->code;
/* iterate to get correct size of constant region */
size_t string_size = 0;
for (size_t i = 0; i < constant->size; ++i) {
struct hasht_node *slot = constant->table[i];
if (slot && !hasht_node_deleted(slot)) {
struct typeinfo *v = slot->value;
if (v->base == FLOAT_T)
string_size += 8;
else if (v->base == CHAR_T && v->pointer)
string_size += v->token->ssize;
}
}
/* print intermediate code file if debugging */
if (arguments.debug) {
char *output_file;
asprintf(&output_file, "%s.ic", filename);
FILE *ic = fopen(output_file, "w");
if (ic == NULL)
log_error("could not save to output file: %s",
output_file);
fprintf(ic, ".file \"%s\"\n", filename);
/* print .string region */
fprintf(ic, ".string %zu\n", string_size);
/* iterate to print everything but ints, chars, and bools */
for (size_t i = 0; i < constant->size; ++i) {
struct hasht_node *slot = constant->table[i];
if (slot && !hasht_node_deleted(slot)) {
struct typeinfo *v = slot->value;
if (v->base == FLOAT_T
|| (v->base == CHAR_T && v->pointer)) {
fprintf(ic, " ");
print_typeinfo(ic, slot->key, v);
fprintf(ic, "\n");
}
}
}
/* print .data region */
fprintf(ic, ".data\n");
for (size_t i = 0; i < global->size; ++i) {
struct hasht_node *slot = global->table[i];
if (slot && !hasht_node_deleted(slot)) {
struct typeinfo *value = slot->value;
if (value->base != FUNCTION_T) {
fprintf(ic, " ");
print_typeinfo(ic, slot->key, value);
fprintf(ic, "\n");
}
}
}
fprintf(ic, ".code\n");
print_code(ic, code);
fclose(ic);
free(output_file);
}
log_debug("generating final code");
/* copy because Wormulon */
char *copy = strdup(filename);
char *base = basename(copy);
free(copy);
char *output_file;
asprintf(&output_file, "%s.c", base);
FILE *fc = fopen(output_file, "w");
if (fc == NULL)
log_error("could not save to output file: %s", output_file);
time_t t = time(NULL);
struct tm *local = localtime(&t);
char timestamp[60];
strftime(timestamp, sizeof(timestamp), "%F %T", local);
fprintf(fc, "/*\n");
fprintf(fc, " * %s - 120++ Three-Address C Code\n", output_file);
fprintf(fc, " * Generated @ %s\n", timestamp);
fprintf(fc, " *\n");
fprintf(fc, " * Created by Andrew Schwartzmeyer's 120++ Compiler\n");
fprintf(fc, " * Project located @ https://github.com/andschwa/uidaho-cs445\n");
fprintf(fc, " */\n\n");
fprintf(fc, "/* Required includes for TAC-C */\n");
fprintf(fc, "#include <stdlib.h>\n");
fprintf(fc, "#include <stdbool.h>\n");
fprintf(fc, "#include <string.h>\n");
if (libs.usingstd && libs.iostream)
fprintf(fc, "#include <stdio.h>\n");
fprintf(fc, "\n");
/* include passed-through C headers */
fprintf(fc, "/* Source-file C headers */\n");
struct list_node *iter = list_head(yyclibs);
while (!list_end(iter)) {
fprintf(fc, "#include %s\n", (char *)iter->data);
iter = iter->next;
}
fprintf(fc, "\n");
/* get maximum param size for faux stack */
size_t max_param_size = 0;
iter = list_head(code);
while (!list_end(iter)) {
struct op *op = iter->data;
if (op->code == PROC_O) {
size_t param_size = op->address[0].offset;
if (param_size > max_param_size)
max_param_size = param_size;
}
iter = iter->next;
}
fprintf(fc, "/* Memory regions */\n");
fprintf(fc, "char constant[%zu];\n", string_size);
fprintf(fc, "char global[%zu];\n", global->size);
fprintf(fc, "char stack[%zu];\n", max_param_size);
fprintf(fc, "\n");
fprintf(fc, "/* Final Three-Address C Generated Code */\n");
final_code(fc, code);
fclose(fc);
/* remove TAC-C "assembler" code */
if (!arguments.assemble) {
/* compile object files */
char *command;
asprintf(&command, "gcc -c %s", output_file);
int status = system(command);
if (status != 0)
log_error("command failed: %s", command);
remove(output_file);
}
free(output_file);
/* clean up */
log_debug("cleaning up");
tree_free(yyprogram);
yylex_destroy();
hasht_free(yytypes);
free(yyincludes); /* values all referenced elsewhere */
list_free(yyfiles);
list_free(yyclibs);
list_free(yyscopes);
}
static error_t parse_opt(int key, char *arg, struct argp_state *state)
{
struct arguments *arguments = state->input;
switch (key) {
case 'd':
arguments->debug = true;
break;
case 't':
arguments->tree = true;
break;
case 'y':
arguments->symbols = true;
break;
case 'k':
arguments->checks = true;
break;
case 'I':
arguments->include = arg;
break;
case 's':
arguments->assemble = true;
break;
case 'c':
arguments->compile = true;
break;
case 'o':
arguments->output = arg;
break;
case ARGP_KEY_NO_ARGS:
argp_usage(state);
case ARGP_KEY_ARGS:
arguments->input_files = &state->argv[state->next];
state->next = state->argc;
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}