source: src/main.cc @ 9e2c1f0

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

Merge branch 'global-init' into ctor and add global destroy function to call destructors on global objects

Conflicts:

src/CodeGen/CodeGenerator.cc
src/InitTweak/module.mk
src/Makefile.in
src/SynTree/Declaration.h
src/SynTree/FunctionDecl.cc
src/main.cc

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