source: src/main.cc @ fac84be

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

add support for constructor/destructor attribute priority and set priority for library constructors to high

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