| 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 "ICGenerator.h"
|
---|
| 10 | #include "SymbolTable.h"
|
---|
| 11 | #include "debug.h"
|
---|
| 12 |
|
---|
| 13 | /* Globals */
|
---|
| 14 | ICGenerator * icg;
|
---|
| 15 | IntermediateCode * icode;
|
---|
| 16 | bool bWarnings = true;
|
---|
| 17 | bool bOptimize1 = false;
|
---|
| 18 | bool extraDump = false;
|
---|
| 19 |
|
---|
| 20 |
|
---|
| 21 | /* Extern functions */
|
---|
| 22 | int yyparse();
|
---|
| 23 |
|
---|
| 24 |
|
---|
| 25 | /* Extern variables */
|
---|
| 26 | extern int lineno;
|
---|
| 27 | extern SyntaxTree * tree;
|
---|
| 28 | extern SymbolTable * symtab;
|
---|
| 29 | extern int gErrorCount;
|
---|
| 30 | extern int gWarningCount;
|
---|
| 31 | extern int msglevel;
|
---|
| 32 |
|
---|
| 33 |
|
---|
| 34 |
|
---|
| 35 |
|
---|
| 36 | void PrintError (char * msg) {
|
---|
| 37 | fprintf (stderr, "Error on line %d: %s\n", lineno, msg);
|
---|
| 38 | gErrorCount++;
|
---|
| 39 | }//PrintError
|
---|
| 40 |
|
---|
| 41 |
|
---|
| 42 | void PrintWarning (char * msg) {
|
---|
| 43 | if (bWarnings) {
|
---|
| 44 | fprintf (stderr, "Warning on line %d: %s\n", lineno, msg);
|
---|
| 45 | gWarningCount++;
|
---|
| 46 | }
|
---|
| 47 | }//PrintWarning
|
---|
| 48 |
|
---|
| 49 | void usage(char * argv0) {
|
---|
| 50 | fprintf (stderr, "Usage: %s [OPTION]\n", argv0);
|
---|
| 51 | fprintf (stderr, "Defaults: %s -w -O0\n", argv0);
|
---|
| 52 | fprintf (stderr, "-h = Reading right now ;-)\n");
|
---|
| 53 | fprintf (stderr, "-w = Enable warnings\n");
|
---|
| 54 | fprintf (stderr, "-O0 = No optimalisation\n");
|
---|
| 55 | fprintf (stderr, "-O1 = Use otimalisation\n");
|
---|
| 56 | fprintf (stderr, "-g = Debug, more flags higher level\n");
|
---|
| 57 | fprintf (stderr, "-s = Dump symbolTable after ICG\n");
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | /* Parse the command line */
|
---|
| 61 | int ParseCommandline(int argc, char ** argv)
|
---|
| 62 | {
|
---|
| 63 | for (int i = 1; i < argc; i++) {
|
---|
| 64 | if (strcmp(argv[i], "-w") == 0) {
|
---|
| 65 | bWarnings = false;
|
---|
| 66 | }
|
---|
| 67 | else if (strcmp(argv[i], "-O0") == 0) {
|
---|
| 68 | bOptimize1 = false;
|
---|
| 69 | }
|
---|
| 70 | else if (strcmp(argv[i], "-O1") == 0) {
|
---|
| 71 | bOptimize1 = true;
|
---|
| 72 | }
|
---|
| 73 | else if (strcmp(argv[i], "-h") == 0) {
|
---|
| 74 | usage(argv[0]);
|
---|
| 75 | return 1;
|
---|
| 76 | }
|
---|
| 77 | else if (strcmp(argv[i], "-g") == 0) {
|
---|
| 78 | msglevel += 30;
|
---|
| 79 | }
|
---|
| 80 | else if (strcmp(argv[i], "-s") == 0) {
|
---|
| 81 | extraDump = true;
|
---|
| 82 | }
|
---|
| 83 | else {
|
---|
| 84 | fprintf(stderr, "Error: unrecognized command line option '%s'\n", argv[i]);
|
---|
| 85 | usage(argv[0]);
|
---|
| 86 | return 1;
|
---|
| 87 | }
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | return 0;
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 |
|
---|
| 94 | void initialize()
|
---|
| 95 | {
|
---|
| 96 | /* Create & initialize the abstract data structures */
|
---|
| 97 | tree = new SyntaxTree;
|
---|
| 98 | symtab = new SymbolTable;
|
---|
| 99 | icg = new ICGenerator;
|
---|
| 100 | icode = NULL;
|
---|
| 101 |
|
---|
| 102 | gErrorCount = 0;
|
---|
| 103 | gWarningCount = 0;
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 |
|
---|
| 107 | void cleanup()
|
---|
| 108 | {
|
---|
| 109 | /* Free objects */
|
---|
| 110 | delete icg;
|
---|
| 111 | delete tree;
|
---|
| 112 | delete symtab;
|
---|
| 113 | if (icode) delete icode;
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 |
|
---|
| 117 | int main(int argc, char ** argv)
|
---|
| 118 | {
|
---|
| 119 | int parseResult;
|
---|
| 120 |
|
---|
| 121 | if (ParseCommandline(argc, argv) == 1) {
|
---|
| 122 | return 1;
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | initialize();
|
---|
| 126 |
|
---|
| 127 | /* Start the parsing procedure */
|
---|
| 128 | parseResult = yyparse();
|
---|
| 129 |
|
---|
| 130 | /* Dump the syntax tree & symbol table */
|
---|
| 131 | tree->Dump (stdout);
|
---|
| 132 | printf("\n");
|
---|
| 133 | symtab->Dump (stdout);
|
---|
| 134 |
|
---|
| 135 | /* Generate intermediate code, if everything is still ok */
|
---|
| 136 | if (!(parseResult || gErrorCount)) {
|
---|
| 137 | if (bOptimize1) {
|
---|
| 138 | icg->Preprocess(tree, symtab);
|
---|
| 139 | tree->Dump (stdout);
|
---|
| 140 | }
|
---|
| 141 | icode = icg->GenerateIntermediateCode(tree, symtab);
|
---|
| 142 | if (bOptimize1) {
|
---|
| 143 | icg->Postprocess(icode, symtab);
|
---|
| 144 | }
|
---|
| 145 | icode->Dump (stdout);
|
---|
| 146 |
|
---|
| 147 | /* Dump Extra symbol table at the end, to view edited variables on the symbol table */
|
---|
| 148 | if (extraDump) {
|
---|
| 149 | printf("\n");
|
---|
| 150 | symtab->Dump (stdout);
|
---|
| 151 | }
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | printf("%d error(s), %d warning(s)\n", gErrorCount + parseResult, gWarningCount);
|
---|
| 155 |
|
---|
| 156 | cleanup();
|
---|
| 157 |
|
---|
| 158 | return (parseResult || gErrorCount);
|
---|
| 159 |
|
---|
| 160 | }
|
---|