source: src/main.cc @ c23f807

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

fix location of builtin.cf at configure, fifth attempt

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