source: src/main.cc @ 71f4e4f

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

added ConstructorInit?, simple constructors and destructors work correctly

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