source: src/main.cc @ d3b7937

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 d3b7937 was d3b7937, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

building runtime library (first attempt)

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