source: src/main.cc @ ca1c11f

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since ca1c11f was ca1c11f, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

added constructor debug flag -c, fixed mistake in removing extraneous destructor calls

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