source: src/main.cc @ 5ead9f9

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 5ead9f9 was cbaee0d, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

more refactoring of parser code

  • Property mode set to 100644
File size: 11.0 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// main.cc --
8//
9// Author           : Richard C. Bilson
10// Created On       : Fri May 15 23:12:02 2015
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Tue Aug 16 21:33:49 2016
13// Update Count     : 260
14//
15
16#include <iostream>
17#include <fstream>
18#include <getopt.h>
19#include "Parser/lex.h"
20#include "Parser/parser.h"
21#include "Parser/TypedefTable.h"
22#include "SynTree/Declaration.h"
23#include "SynTree/Visitor.h"
24#include "GenPoly/Lvalue.h"
25#include "GenPoly/Specialize.h"
26#include "GenPoly/Box.h"
27#include "GenPoly/CopyParams.h"
28#include "GenPoly/InstantiateGeneric.h"
29#include "CodeGen/Generate.h"
30#include "CodeGen/FixNames.h"
31#include "ControlStruct/Mutate.h"
32#include "Tuples/Mutate.h"
33#include "Tuples/FunctionChecker.h"
34#include "SymTab/Mangler.h"
35#include "SymTab/Indexer.h"
36#include "SymTab/Validate.h"
37#include "ResolvExpr/AlternativePrinter.h"
38#include "ResolvExpr/Resolver.h"
39#include "MakeLibCfa.h"
40#include "InitTweak/Mutate.h"
41#include "InitTweak/GenInit.h"
42#include "InitTweak/FixInit.h"
43
44#include "Common/SemanticError.h"
45#include "Common/UnimplementedError.h"
46
47#include "../config.h"
48
49using namespace std;
50
51#define OPTPRINT(x) if ( errorp ) std::cerr << x << std::endl;
52
53extern int yydebug;
54
55LinkageSpec::Type linkage = LinkageSpec::Cforall;
56TypedefTable typedefTable;
57DeclarationNode * parseTree = nullptr;                                  // program parse tree
58
59bool
60        astp = false,
61        bresolvep = false,
62        bboxp = false,
63        ctorinitp = false,
64        exprp = false,
65        expraltp = false,
66        grammarp = false,
67        libcfap = false,
68        nopreludep = false,
69        noprotop = false,
70        parsep = false,
71        resolvep = false,                                                                       // used in AlternativeFinder
72        symtabp = false,
73        treep = false,
74        validp = false,
75        errorp = false,
76        codegenp = false;
77
78static void parse( FILE * input, LinkageSpec::Type t, bool shouldExit = false );
79static void dump( std::list< Declaration * > & translationUnit, std::ostream & out = std::cout );
80static void parse_cmdline( int argc, char *argv[], const char *& filename );
81
82
83int main( int argc, char * argv[] ) {
84        FILE * input;
85        std::ostream *output = & std::cout;
86        std::list< Declaration * > translationUnit;
87        const char *filename = NULL;
88
89        parse_cmdline( argc, argv, filename );                          // process command-line arguments
90
91        try {
92                // choose to read the program from a file or stdin
93                if ( optind < argc ) {
94                        input = fopen( argv[ optind ], "r" );
95                        if ( ! input ) {
96                                std::cout << "Error: cannot open " << argv[ optind ] << std::endl;
97                                exit( EXIT_FAILURE );
98                        } // if
99                        // if running cfa-cpp directly, might forget to pass -F option (and really shouldn't have to)
100                        if ( filename == NULL ) filename = argv[ optind ];
101                        // prelude filename comes in differently
102                        if ( libcfap ) filename = "prelude.cf";
103                        optind += 1;
104                } else {
105                        input = stdin;
106                        // if running cfa-cpp directly, might forget to pass -F option. Since this takes from stdin, pass
107                        // a fake name along
108                        if ( filename == NULL ) filename = "stdin";
109                } // if
110
111                if ( optind < argc ) {
112                        output = new ofstream( argv[ optind ] );
113                } // if
114
115                yydebug = grammarp;
116
117                // read in the builtins, extras, and the prelude
118                if ( ! nopreludep ) {                                                   // include gcc builtins
119                        // -l is for initial build ONLY and builtins.cf is not in the lib directory so access it here.
120                        FILE * builtins = fopen( libcfap | treep ? "builtins.cf" : CFA_LIBDIR "/builtins.cf", "r" );
121                        if ( builtins == NULL ) {
122                                std::cerr << "Error: cannot open builtins.cf" << std::endl;
123                                exit( EXIT_FAILURE );
124                        } // if
125                        parse( builtins, LinkageSpec::Compiler );
126
127                        // read the extra prelude in, if not generating the cfa library
128                        FILE * extras = fopen( libcfap | treep ? "extras.cf" : CFA_LIBDIR "/extras.cf", "r" );
129                        if ( extras == NULL ) {
130                                std::cerr << "Error: cannot open extras.cf" << std::endl;
131                                exit( EXIT_FAILURE );
132                        } // if
133                        parse( extras, LinkageSpec::C );
134
135                        if ( ! libcfap ) {
136                                // read the prelude in, if not generating the cfa library
137                                FILE * prelude = fopen( treep ? "prelude.cf" : CFA_LIBDIR "/prelude.cf", "r" );
138                                if ( prelude == NULL ) {
139                                        std::cerr << "Error: cannot open prelude.cf" << std::endl;
140                                        exit( EXIT_FAILURE );
141                                } // if
142
143                                parse( prelude, LinkageSpec::Intrinsic );
144                        } // if
145                } // if
146
147                parse( input, libcfap ? LinkageSpec::Intrinsic : LinkageSpec::Cforall, grammarp );
148
149                if ( parsep ) {
150                        parseTree->printList( std::cout );
151                        delete parseTree;
152                        return 0;
153                } // if
154
155                buildList( parseTree, translationUnit );
156                delete parseTree;
157                parseTree = nullptr;
158
159                if ( astp ) {
160                        dump( translationUnit );
161                        return 0;
162                } // if
163
164                // add the assignment statement after the initialization of a type parameter
165                OPTPRINT( "validate" )
166                SymTab::validate( translationUnit, symtabp );
167                if ( symtabp ) {
168                        return 0;
169                } // if
170
171                if ( expraltp ) {
172                        ResolvExpr::AlternativePrinter printer( std::cout );
173                        acceptAll( translationUnit, printer );
174                        return 0;
175                } // if
176
177                if ( validp ) {
178                        dump( translationUnit );
179                        return 0;
180                } // if
181
182                OPTPRINT( "mutate" )
183                ControlStruct::mutate( translationUnit );
184                OPTPRINT( "fixNames" )
185                CodeGen::fixNames( translationUnit );
186                OPTPRINT( "tweakInit" )
187                InitTweak::genInit( translationUnit );
188
189                if ( libcfap ) {
190                        // generate the bodies of cfa library functions
191                        LibCfa::makeLibCfa( translationUnit );
192                } // if
193
194                if ( bresolvep ) {
195                        dump( translationUnit );
196                        return 0;
197                } // if
198
199                OPTPRINT( "resolve" )
200                ResolvExpr::resolve( translationUnit );
201                if ( exprp ) {
202                        dump( translationUnit );
203                        return 0;
204                }
205
206                // fix ObjectDecl - replaces ConstructorInit nodes
207                OPTPRINT( "fixInit" )
208                InitTweak::fix( translationUnit, filename, libcfap || treep );
209                if ( ctorinitp ) {
210                        dump ( translationUnit );
211                        return 0;
212                }
213
214                OPTPRINT("instantiateGenerics")
215                GenPoly::instantiateGeneric( translationUnit );
216                OPTPRINT( "copyParams" );
217                GenPoly::copyParams( translationUnit );
218                OPTPRINT( "convertSpecializations" )
219                GenPoly::convertSpecializations( translationUnit );
220                OPTPRINT( "convertLvalue" )
221                GenPoly::convertLvalue( translationUnit );
222
223                if ( bboxp ) {
224                        dump( translationUnit );
225                        return 0;
226                }
227                OPTPRINT( "box" )
228                GenPoly::box( translationUnit );
229
230                // print tree right before code generation
231                if ( codegenp ) {
232                        dump( translationUnit );
233                        return 0;
234                } // if
235
236                CodeGen::generate( translationUnit, *output, ! noprotop );
237
238                if ( output != &std::cout ) {
239                        delete output;
240                } // if
241        } catch ( SemanticError &e ) {
242                if ( errorp ) {
243                        std::cerr << "---AST at error:---" << std::endl;
244                        dump( translationUnit, std::cerr );
245                        std::cerr << std::endl << "---End of AST, begin error message:---\n" << std::endl;
246                }
247                e.print( std::cerr );
248                if ( output != &std::cout ) {
249                        delete output;
250                } // if
251                return 1;
252        } catch ( UnimplementedError &e ) {
253                std::cout << "Sorry, " << e.get_what() << " is not currently implemented" << std::endl;
254                if ( output != &std::cout ) {
255                        delete output;
256                } // if
257                return 1;
258        } catch ( CompilerError &e ) {
259                std::cerr << "Compiler Error: " << e.get_what() << std::endl;
260                std::cerr << "(please report bugs to " << std::endl;
261                if ( output != &std::cout ) {
262                        delete output;
263                } // if
264                return 1;
265        } // try
266
267        deleteAll( translationUnit );
268        return 0;
269} // main
270
271void parse_cmdline( int argc, char * argv[], const char *& filename ) {
272        enum { Ast, Bbox, Bresolver, CtorInitFix, Expr, ExprAlt, Grammar, LibCFA, Nopreamble, Parse, Prototypes, Resolver, Symbol, Tree, Validate, };
273
274        static struct option long_opts[] = {
275                { "ast", no_argument, 0, Ast },
276                { "before-box", no_argument, 0, Bbox },
277                { "before-resolver", no_argument, 0, Bresolver },
278                { "ctorinitfix", no_argument, 0, CtorInitFix },
279                { "expr", no_argument, 0, Expr },
280                { "expralt", no_argument, 0, ExprAlt },
281                { "grammar", no_argument, 0, Grammar },
282                { "libcfa", no_argument, 0, LibCFA },
283                { "no-preamble", no_argument, 0, Nopreamble },
284                { "parse", no_argument, 0, Parse },
285                { "no-prototypes", no_argument, 0, Prototypes },
286                { "resolver", no_argument, 0, Resolver },
287                { "symbol", no_argument, 0, Symbol },
288                { "tree", no_argument, 0, Tree },
289                { "validate", no_argument, 0, Validate },
290                { 0, 0, 0, 0 }
291        }; // long_opts
292        int long_index;
293
294        opterr = 0;                                                                                     // (global) prevent getopt from printing error messages
295
296        int c;
297        while ( (c = getopt_long( argc, argv, "abBcefglnpqrstvyzD:F:", long_opts, &long_index )) != -1 ) {
298                switch ( c ) {
299                  case Ast:
300                  case 'a':                                                                             // dump AST
301                        astp = true;
302                        break;
303                  case Bresolver:
304                  case 'b':                                                                             // print before resolver steps
305                        bresolvep = true;
306                        break;
307                  case 'B':                                                                             // print before resolver steps
308                        bboxp = true;
309                        break;
310                  case CtorInitFix:
311                  case 'c':
312                        ctorinitp = true;
313                        break;
314                  case Expr:
315                  case 'e':                                                                             // dump AST after expression analysis
316                        exprp = true;
317                        break;
318                  case ExprAlt:
319                  case 'f':                                                                             // print alternatives for expressions
320                        expraltp = true;
321                        break;
322                  case Grammar:
323                  case 'g':                                                                             // bison debugging info (grammar rules)
324                        grammarp = true;
325                        break;
326                  case LibCFA:
327                  case 'l':                                                                             // generate libcfa.c
328                        libcfap = true;
329                        break;
330                  case Nopreamble:
331                  case 'n':                                                                             // do not read preamble
332                        nopreludep = true;
333                        break;
334                  case Prototypes:
335                  case 'p':                                                                             // generate prototypes for preamble functions
336                        noprotop = true;
337                        break;
338                  case Parse:
339                  case 'q':                                                                             // dump parse tree
340                        parsep = true;
341                        break;
342                  case Resolver:
343                  case 'r':                                                                             // print resolver steps
344                        resolvep = true;
345                        break;
346                  case Symbol:
347                  case 's':                                                                             // print symbol table events
348                        symtabp = true;
349                        break;
350                  case Tree:
351                  case 't':                                                                             // build in tree
352                        treep = true;
353                        break;
354                  case 'v':                                                                             // dump AST after decl validation pass
355                        validp = true;
356                        break;
357                  case 'y':
358                        errorp = true;
359                        break;
360                  case 'z':
361                        codegenp = true;
362                        break;
363                  case 'D':                                                                             // ignore -Dxxx
364                        break;
365                  case 'F':                                                                             // source file-name without suffix
366                        filename = optarg;
367                        break;
368                  case '?':
369                        cout << "Unknown option: '" << (char)optopt << "'" << endl;
370                        exit( EXIT_FAILURE );
371                  default:
372                        abort();
373                } // switch
374        } // while
375} // parse_cmdline
376
377static void parse( FILE * input, LinkageSpec::Type link, bool shouldExit ) {
378        extern int yyparse( void );
379        extern FILE * yyin;
380        extern int yylineno;
381
382        linkage = link;                                                                         // set globals
383        yyin = input;
384        yylineno = 1;
385        typedefTable.enterScope();
386        int parseStatus = yyparse();
387
388        fclose( input );
389        if ( shouldExit || parseStatus != 0 ) {
390                exit( parseStatus );
391        } // if
392} // parse
393
394static bool notPrelude( Declaration * decl ) {
395        return ! LinkageSpec::isBuiltin( decl->get_linkage() );
396} // notPrelude
397
398static void dump( std::list< Declaration * > & translationUnit, std::ostream & out ) {
399        std::list< Declaration * > decls;
400        if ( noprotop ) {
401                filter( translationUnit.begin(), translationUnit.end(), std::back_inserter( decls ), notPrelude );
402        } else {
403                decls = translationUnit;
404        }
405
406        printAll( decls, out );
407        deleteAll( translationUnit );
408} // dump
409
410// Local Variables: //
411// tab-width: 4 //
412// mode: c++ //
413// compile-command: "make install" //
414// End:  //
Note: See TracBrowser for help on using the repository browser.