source: src/main.cc @ cb4c607

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 cb4c607 was 7b937575, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

pass source file-name without suffix to cfa-cpp

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