1 | /*
|
---|
2 | * main.cc
|
---|
3 | *
|
---|
4 | * This file contains some generic functions. It is part of
|
---|
5 | * the assignment for the Compiler Construction course at the Leiden Institute
|
---|
6 | * of Advanced Computer Science (LIACS), Leiden University.
|
---|
7 | */
|
---|
8 |
|
---|
9 | #include "SymbolTable.h"
|
---|
10 | #include "ICGenerator.h"
|
---|
11 | #include "CodeGenerator.h"
|
---|
12 |
|
---|
13 | /* Globals */
|
---|
14 | ICGenerator * icg;
|
---|
15 | IntermediateCode * icode;
|
---|
16 | CodeGenerator * cg;
|
---|
17 | bool bWarnings = true;
|
---|
18 | bool bOptimize1 = false;
|
---|
19 |
|
---|
20 |
|
---|
21 | /* Extern functions */
|
---|
22 | int yyparse(); // The parser
|
---|
23 | bool IsGlobal(Symbol * sym); // Is the specified symbol a global variable?
|
---|
24 |
|
---|
25 |
|
---|
26 | /* Extern variables */
|
---|
27 | extern int lineno;
|
---|
28 | extern SyntaxTree * tree;
|
---|
29 | extern SymbolTable * symtab;
|
---|
30 | extern int gErrorCount;
|
---|
31 | extern int gWarningCount;
|
---|
32 |
|
---|
33 |
|
---|
34 |
|
---|
35 | void PrintError (char * msg) {
|
---|
36 | fprintf (stderr, "Error on line %d: %s\n", lineno, msg);
|
---|
37 | gErrorCount++;
|
---|
38 | }//PrintError
|
---|
39 |
|
---|
40 |
|
---|
41 | void PrintWarning (char * msg) {
|
---|
42 | if (bWarnings) {
|
---|
43 | fprintf (stderr, "Warning on line %d: %s\n", lineno, msg);
|
---|
44 | gWarningCount++;
|
---|
45 | }
|
---|
46 | }//PrintWarning
|
---|
47 |
|
---|
48 |
|
---|
49 | /* Parse the command line */
|
---|
50 | int ParseCommandline(int argc, char ** argv)
|
---|
51 | {
|
---|
52 | for (int i = 1; i < argc; i++) {
|
---|
53 | if (strcmp(argv[i], "-w") == 0) {
|
---|
54 | bWarnings = false;
|
---|
55 | }
|
---|
56 | else if (strcmp(argv[i], "-O0") == 0) {
|
---|
57 | bOptimize1 = false;
|
---|
58 | }
|
---|
59 | else if (strcmp(argv[i], "-O1") == 0) {
|
---|
60 | bOptimize1 = true;
|
---|
61 | }
|
---|
62 | else {
|
---|
63 | fprintf(stderr, "Error: unrecognized command line option '%s'\n", argv[i]);
|
---|
64 | return 1;
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | return 0;
|
---|
69 | }
|
---|
70 |
|
---|
71 |
|
---|
72 | void initialize()
|
---|
73 | {
|
---|
74 | /* Create & initialize the abstract data structures */
|
---|
75 | tree = new SyntaxTree;
|
---|
76 | symtab = new SymbolTable;
|
---|
77 | icg = new ICGenerator;
|
---|
78 | cg = new CodeGenerator;
|
---|
79 | icode = NULL;
|
---|
80 |
|
---|
81 | gErrorCount = 0;
|
---|
82 | gWarningCount = 0;
|
---|
83 | }
|
---|
84 |
|
---|
85 |
|
---|
86 | void cleanup()
|
---|
87 | {
|
---|
88 | /* Free objects */
|
---|
89 | delete cg;
|
---|
90 | delete icg;
|
---|
91 | delete tree;
|
---|
92 | delete symtab;
|
---|
93 | if (icode) delete icode;
|
---|
94 | }
|
---|
95 |
|
---|
96 |
|
---|
97 | int main(int argc, char ** argv)
|
---|
98 | {
|
---|
99 | FILE * outfile;
|
---|
100 | const char * outfilename = "out.s";
|
---|
101 | int parseResult;
|
---|
102 |
|
---|
103 | if (ParseCommandline(argc, argv) == 1) {
|
---|
104 | return 1;
|
---|
105 | }
|
---|
106 |
|
---|
107 | initialize();
|
---|
108 |
|
---|
109 | /* Start the parsing procedure */
|
---|
110 | parseResult = yyparse();
|
---|
111 |
|
---|
112 | /* Dump the syntax tree & symbol table */
|
---|
113 | tree->Dump (stdout);
|
---|
114 | printf("\n");
|
---|
115 | symtab->Dump (stdout);
|
---|
116 |
|
---|
117 | /* Generate intermediate code, if everything is still ok */
|
---|
118 | if (!(parseResult || gErrorCount)) {
|
---|
119 | if (bOptimize1) {
|
---|
120 | icg->Preprocess(tree, symtab);
|
---|
121 | }
|
---|
122 | icode = icg->GenerateIntermediateCode(tree, symtab);
|
---|
123 | icode->Dump(stdout);
|
---|
124 | if (bOptimize1) {
|
---|
125 | icg->Postprocess(icode, symtab);
|
---|
126 | }
|
---|
127 |
|
---|
128 | /* Now generate the final assembly */
|
---|
129 | outfile = fopen(outfilename, "w+");
|
---|
130 | cg->GenerateHeader(outfile);
|
---|
131 | cg->GenerateGlobalDecls(outfile, symtab);
|
---|
132 | cg->GenerateCode(outfile, symtab, icode);
|
---|
133 | cg->GenerateTrailer(outfile);
|
---|
134 | cg->Dump(outfile);
|
---|
135 | fclose(outfile);
|
---|
136 | printf("Output has been written to \"%s\".\n", outfilename);
|
---|
137 | }
|
---|
138 |
|
---|
139 | printf("%d error(s), %d warning(s)\n", gErrorCount + parseResult, gWarningCount);
|
---|
140 |
|
---|
141 | cleanup();
|
---|
142 |
|
---|
143 | return (parseResult || gErrorCount);
|
---|
144 |
|
---|
145 | }
|
---|