source: src/main.cc @ f6d7e0f

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 f6d7e0f was f6d7e0f, checked in by Rob Schluntz <rschlunt@…>, 9 years ago

label fix, enumeration assignment first attempt

  • Property mode set to 100644
File size: 8.4 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<<<<<<< Updated upstream
12// Last Modified By : Peter A. Buhr
13// Last Modified On : Mon Jun 29 17:09:21 2015
14// Update Count     : 77
15=======
16// Last Modified By : Rob Schluntz
17// Last Modified On : Fri Jul 03 13:08:52 2015
18// Update Count     : 75
19>>>>>>> Stashed changes
20//
21
22#include <iostream>
23#include <fstream>
24#include <cstdlib>
25#include <cstdio>
26#include <getopt.h>
27#include "Parser/Parser.h"
28#include "Parser/ParseNode.h"
29#include "Parser/LinkageSpec.h"
30#include "SynTree/Declaration.h"
31#include "SynTree/Visitor.h"
32#include "GenPoly/Lvalue.h"
33#include "GenPoly/Specialize.h"
34#include "GenPoly/Box.h"
35#include "GenPoly/CopyParams.h"
36#include "CodeGen/Generate.h"
37#include "CodeGen/FixNames.h"
38#include "ControlStruct/Mutate.h"
39#include "Tuples/Mutate.h"
40#include "Tuples/FunctionChecker.h"
41#include "SymTab/Mangler.h"
42#include "SymTab/Indexer.h"
43#include "SymTab/Validate.h"
44#include "ResolvExpr/AlternativePrinter.h"
45#include "ResolvExpr/Resolver.h"
46#include "MakeLibCfa.h"
47#include "InitTweak/Mutate.h"
48#include "InitTweak/RemoveInit.h"
49//#include "Explain/GenProlog.h"
50//#include "Try/Visit.h"
51
52#include "SemanticError.h"
53#include "UnimplementedError.h"
54
55#include "../config.h"
56
57using namespace std;
58
59#define OPTPRINT(x) \
60        if ( errorp ) std::cerr << x << std::endl;
61
62void parse( FILE * input, LinkageSpec::Type t, bool shouldExit = false );
63
64bool
65        astp = false,
66        bresolvep = false,
67        exprp = false,
68        expraltp = false,
69        grammarp = false,
70        libcfap = false,
71        nopreludep = false,
72        protop = false,
73        parsep = false,
74        resolvep = false,                                                                       // used in AlternativeFinder
75        symtabp = false,
76        validp = false,
77        errorp = false,
78        codegenp = false;
79
80enum { Ast, Bresolver, Expr, ExprAlt, Grammar, LibCFA, Nopreamble, Parse, Prototypes, Resolver, Symbol, Validate, };
81
82static struct option long_opts[] = {
83        { "ast", no_argument, 0, Ast },
84        { "before-resolver", no_argument, 0, Bresolver },
85        { "expr", no_argument, 0, Expr },
86        { "expralt", no_argument, 0, ExprAlt },
87        { "grammar", no_argument, 0, Grammar },
88        { "libcfa", no_argument, 0, LibCFA },
89        { "nopreamble", no_argument, 0, Nopreamble },
90        { "parse", no_argument, 0, Parse },
91        { "prototypes", no_argument, 0, Prototypes },
92        { "resolver", no_argument, 0, Resolver },
93        { "symbol", no_argument, 0, Symbol },
94        { "validate", no_argument, 0, Validate },
95        { 0, 0, 0, 0 }
96};
97
98int main( int argc, char *argv[] ) {
99        FILE *input;
100        std::ostream *output = &std::cout;
101        int long_index;
102        std::list< Declaration* > translationUnit;
103
104        opterr = 0;                                                                                     // prevent getopt from printing error messages
105       
106        int c;
107        while ( (c = getopt_long( argc, argv, "abefglnpqrsvyzD:", long_opts, &long_index )) != -1 ) {
108                switch ( c ) {
109                  case Ast:
110                  case 'a':                                                                             // dump AST
111                        astp = true;
112                        break;
113                  case Bresolver:
114                  case 'b':                                                                             // print before resolver steps
115                        bresolvep = true;
116                        break;
117                  case Expr:
118                  case 'e':                                                                             // dump AST after expression analysis
119                        exprp = true;
120                        break;
121                  case ExprAlt:
122                  case 'f':                                                                             // print alternatives for expressions
123                        expraltp = true;
124                        break;
125                  case Grammar:
126                  case 'g':                                                                             // bison debugging info (grammar rules)
127                        grammarp = true;
128                        break;
129                  case LibCFA:
130                  case 'l':                                                                             // generate libcfa.c
131                        libcfap = true;
132                        break;
133                  case Nopreamble:
134                  case 'n':                                                                             // do not read preamble
135                        nopreludep = true;
136                        break;
137                  case Prototypes:
138                  case 'p':                                                                             // generate prototypes for preamble functions
139                        protop = true;
140                        break;
141                  case Parse:
142                  case 'q':                                                                             // dump parse tree
143                        parsep = true;
144                        break;
145                  case Resolver:
146                  case 'r':                                                                             // print resolver steps
147                        resolvep = true;
148                        break;
149                  case Symbol:
150                  case 's':                                                                             // print symbol table events
151                        symtabp = true;
152                        break;
153                  case 'v':                                                                             // dump AST after decl validation pass
154                        validp = true;
155                        break;
156                  case 'y':
157                        errorp = true;
158                        break;
159                  case 'z':
160                        codegenp = true;
161                        break;
162                  case 'D':                                                                             // ignore -Dxxx
163                        break;
164                  case '?':
165                        cout << "Unknown option: '" << (char)optopt << "'" << endl;
166                        exit(1);
167                  default:
168                        abort();
169                } // switch
170        } // while
171
172        try {
173                // choose to read the program from a file or stdin
174                if ( optind < argc ) {
175                        input = fopen( argv[ optind ], "r" );
176                        if ( ! input ) {
177                                std::cout << "Error: can't open " << argv[optind] << std::endl;
178                                exit( 1 );
179                        } // if
180                        optind += 1;
181                } else {
182                        input = stdin;
183                } // if
184
185                if ( optind < argc ) {
186                        output = new ofstream( argv[ optind ] );
187                } // if
188       
189                Parser::get_parser().set_debug( grammarp );
190
191                // read in the builtins and the prelude
192                if ( ! nopreludep ) {                                                   // include gcc builtins
193                        FILE * builtins = fopen( CFA_LIBDIR "/builtins.cf", "r" );
194                        if ( builtins == NULL ) {
195                                std::cerr << "Error: can't open builtins" << std::endl;
196                                exit( 1 );
197                        } // if
198
199                        parse( builtins, LinkageSpec::Compiler );
200
201                        if ( ! libcfap ) {
202                                // read the prelude in, if we're not generating the cfa library
203                                FILE * prelude = fopen( CFA_LIBDIR "/prelude.cf", "r" );
204                                if ( prelude == NULL ) {
205                                        std::cerr << "Error: can't open prelude" << std::endl;
206                                        exit( 1 );
207                                } // if
208                   
209                    parse( prelude, LinkageSpec::Intrinsic );
210                        } // if
211                } // if
212
213                if ( libcfap ) {
214                        parse( input, LinkageSpec::Intrinsic ); 
215                } else {
216                        parse( input, LinkageSpec::Cforall, grammarp ); 
217                }
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                        printAll( translationUnit, std::cout );
230                        return 0;
231                } // if
232
233                // add the assignment statement after the
234                // initialization of a type parameter
235                OPTPRINT( "tweak" )
236                InitTweak::tweak( translationUnit );
237                OPTPRINT( "validate" )
238                SymTab::validate( translationUnit, symtabp );
239                if ( symtabp ) {
240                        return 0;
241                } // if
242
243                if ( expraltp ) {
244                        ResolvExpr::AlternativePrinter printer( std::cout );
245                        acceptAll( translationUnit, printer );
246                        return 0;
247                } // if
248
249                if ( validp ) {
250                        printAll( translationUnit, std::cout );
251                        return 0;
252                } // if
253
254                OPTPRINT( "mutate" )
255                ControlStruct::mutate( translationUnit );
256                OPTPRINT( "fixNames" ) 
257                CodeGen::fixNames( translationUnit );
258
259                if ( libcfap ) {
260                        protop = true;
261                        // generate the bodies of cfa library functions
262                        LibCfa::makeLibCfa( translationUnit );
263                } // if
264
265                if ( bresolvep ) {
266                        printAll( translationUnit, std::cout );
267                        return 0;
268                } // if
269
270                OPTPRINT( "resolve" )
271                ResolvExpr::resolve( translationUnit );
272                if ( exprp ) {
273                        printAll( translationUnit, std::cout );
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                        printAll( translationUnit, std::cout );
288                        return 0;
289                } // if
290
291                CodeGen::generate( translationUnit, *output, true ); //protop );
292
293                if ( output != &std::cout ) {
294                        delete output;
295                } // if
296        } catch ( SemanticError &e ) {
297                if ( errorp ) {
298                        printAll( translationUnit, std::cerr );
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
323void 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
333// Local Variables: //
334// tab-width: 4 //
335// mode: c++ //
336// compile-command: "make install" //
337// End:  //
Note: See TracBrowser for help on using the repository browser.