source: src/main.cc @ f1e012b

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

added intrinsic ctor/dtors to prelude, modified MakeLibCfa? to build prelude ctor/dtors, added ctor/dtor to polymorphic object type constraints, rudimentary fallback on initializer nodes if chosen ctor is intrinsic, remove intrinsic destructor statements to reduce output pollution

  • 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 : Tue Jan 19 13:16:23 2016
13// Update Count     : 185
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( "tweakInit" )
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                OPTPRINT( "fixInit" )
269                // fix ObjectDecl - replaces ConstructorInit nodes
270                InitTweak::fix( translationUnit );
271
272                OPTPRINT( "instantiateGeneric" )
273                GenPoly::instantiateGeneric( translationUnit );
274                OPTPRINT( "copyParams" );
275                GenPoly::copyParams( translationUnit );
276                OPTPRINT( "convertSpecializations" )
277                GenPoly::convertSpecializations( translationUnit );
278                OPTPRINT( "convertLvalue" )
279                GenPoly::convertLvalue( translationUnit );
280                OPTPRINT( "box" )
281                GenPoly::box( translationUnit );
282
283                // print tree right before code generation
284                if ( codegenp ) {
285                        dump( translationUnit );
286                        return 0;
287                } // if
288
289                CodeGen::generate( translationUnit, *output, ! noprotop );
290
291                if ( output != &std::cout ) {
292                        delete output;
293                } // if
294        } catch ( SemanticError &e ) {
295                if ( errorp ) {
296                        dump( translationUnit );
297                }
298                e.print( std::cerr );
299                if ( output != &std::cout ) {
300                        delete output;
301                } // if
302                return 1;
303        } catch ( UnimplementedError &e ) {
304                std::cout << "Sorry, " << e.get_what() << " is not currently implemented" << std::endl;
305                if ( output != &std::cout ) {
306                        delete output;
307                } // if
308                return 1;
309        } catch ( CompilerError &e ) {
310                std::cerr << "Compiler Error: " << e.get_what() << std::endl;
311                std::cerr << "(please report bugs to " << std::endl;
312                if ( output != &std::cout ) {
313                        delete output;
314                } // if
315                return 1;
316        } // try
317
318        return 0;
319} // main
320
321static void parse( FILE * input, LinkageSpec::Type linkage, bool shouldExit ) {
322        Parser::get_parser().set_linkage( linkage );
323        Parser::get_parser().parse( input );
324
325        fclose( input );
326        if ( shouldExit || Parser::get_parser().get_parseStatus() != 0 ) {
327                exit( Parser::get_parser().get_parseStatus() );
328        } // if
329}
330
331static bool notPrelude( Declaration * decl ) {
332        return ! LinkageSpec::isBuiltin( decl->get_linkage() );
333}
334
335static void dump( std::list< Declaration * > & translationUnit ) {
336        std::list< Declaration * > decls;
337        if ( noprotop ) {
338                filter( translationUnit.begin(), translationUnit.end(),
339                                std::back_inserter( decls ), notPrelude );
340        } else {
341                decls = translationUnit;
342        }
343
344        printAll( decls, std::cout );
345        deleteAll( translationUnit );
346}
347
348
349// Local Variables: //
350// tab-width: 4 //
351// mode: c++ //
352// compile-command: "make install" //
353// End:  //
Note: See TracBrowser for help on using the repository browser.