source: src/main.cc @ 926af74

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 926af74 was 926af74, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

more refactoring of parser code

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