source: src/main.cc @ a1d6d80

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

Account for lvalue returning functions in FixCopyCtor?, removed ConditionalExpr?, CommaExpr?, Logical Expr mutates from Specialize, added before box debug flag

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