source: src/main.cc @ 7bcf74e

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

fix parsing error for EOF message, change cfa-ccp -x flag to -v, regression testing third attempt: consolidate example programs

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