source: src/main.cc @ 644ec6a

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 644ec6a was 159c62e, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

add small set of library types/routines to prelude to reduce including larger library files

  • Property mode set to 100644
File size: 10.7 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//
[71f4e4f]7// main.cc --
[b87a5ed]8//
[843054c2]9// Author           : Richard C. Bilson
[b87a5ed]10// Created On       : Fri May 15 23:12:02 2015
[dd51906]11// Last Modified By : Peter A. Buhr
[159c62e]12// Last Modified On : Tue Jul  5 15:23:11 2016
13// Update Count     : 209
[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"
[a0fdbd5]42#include "InitTweak/GenInit.h"
[71f4e4f]43#include "InitTweak/FixInit.h"
[711eee5]44#include "InitTweak/FixGlobalInit.h"
[51b7345]45//#include "Explain/GenProlog.h"
46//#include "Try/Visit.h"
47
[d3b7937]48#include "Common/SemanticError.h"
49#include "Common/UnimplementedError.h"
[51b7345]50
51#include "../config.h"
52
53using namespace std;
54
[81419b5]55#define OPTPRINT(x) \
56        if ( errorp ) std::cerr << x << std::endl;
57
[1ab4ce2]58static void parse( FILE * input, LinkageSpec::Type t, bool shouldExit = false );
[f77f12e2]59static void dump( std::list< Declaration * > & translationUnit, std::ostream & out = std::cout );
[81419b5]60
[b87a5ed]61bool
62        astp = false,
[de62360d]63        bresolvep = false,
[fea7ca7]64        bboxp = false,
[ca1c11f]65        ctorinitp = false,
[b87a5ed]66        exprp = false,
67        expraltp = false,
68        grammarp = false,
69        libcfap = false,
[de62360d]70        nopreludep = false,
[1ab4ce2]71        noprotop = false,
[de62360d]72        parsep = false,
[b87a5ed]73        resolvep = false,                                                                       // used in AlternativeFinder
74        symtabp = false,
[d3b7937]75        treep = false,
[b87a5ed]76        validp = false,
[de62360d]77        errorp = false,
78        codegenp = false;
[b87a5ed]79
[fea7ca7]80enum { Ast, Bbox, Bresolver, CtorInitFix, Expr, ExprAlt, Grammar, LibCFA, Nopreamble, Parse, Prototypes, Resolver, Symbol, Tree, Validate, };
[b87a5ed]81
82static struct option long_opts[] = {
83        { "ast", no_argument, 0, Ast },
[fea7ca7]84        { "before-box", no_argument, 0, Bbox },
[de62360d]85        { "before-resolver", no_argument, 0, Bresolver },
[ca1c11f]86        { "ctorinitfix", no_argument, 0, CtorInitFix },
[b87a5ed]87        { "expr", no_argument, 0, Expr },
88        { "expralt", no_argument, 0, ExprAlt },
89        { "grammar", no_argument, 0, Grammar },
90        { "libcfa", no_argument, 0, LibCFA },
[1ab4ce2]91        { "no-preamble", no_argument, 0, Nopreamble },
[b1d6dd5]92        { "parse", no_argument, 0, Parse },
[1ab4ce2]93        { "no-prototypes", no_argument, 0, Prototypes },
[b87a5ed]94        { "resolver", no_argument, 0, Resolver },
95        { "symbol", no_argument, 0, Symbol },
[d3b7937]96        { "tree", no_argument, 0, Tree },
[b1d6dd5]97        { "validate", no_argument, 0, Validate },
[b87a5ed]98        { 0, 0, 0, 0 }
99};
[51b7345]100
[8c17ab0]101int main( int argc, char *argv[] ) {
[b87a5ed]102        FILE *input;
103        std::ostream *output = &std::cout;
104        int long_index;
[7b937575]105        std::list< Declaration * > translationUnit;
[d029162]106        const char *filename = NULL;
[b87a5ed]107
108        opterr = 0;                                                                                     // prevent getopt from printing error messages
[71f4e4f]109
[b87a5ed]110        int c;
[9e2c1f0]111        while ( (c = getopt_long( argc, argv, "abBcefglnpqrstvyzD:F:", long_opts, &long_index )) != -1 ) {
[b87a5ed]112                switch ( c ) {
113                  case Ast:
114                  case 'a':                                                                             // dump AST
115                        astp = true;
116                        break;
[de62360d]117                  case Bresolver:
118                  case 'b':                                                                             // print before resolver steps
119                        bresolvep = true;
120                        break;
[fea7ca7]121                  case 'B':                                                                             // print before resolver steps
122                        bboxp = true;
123                        break;
124                  case CtorInitFix:
125                  case 'c':
[ca1c11f]126                        ctorinitp = true;
127                        break;
[b87a5ed]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
[de62360d]146                        nopreludep = true;
[b87a5ed]147                        break;
148                  case Prototypes:
149                  case 'p':                                                                             // generate prototypes for preamble functions
[1ab4ce2]150                        noprotop = true;
[b87a5ed]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;
[d3b7937]164                  case Tree:
165                  case 't':                                                                             // build in tree
166                        treep = true;
167                        break;
[b1d6dd5]168                  case 'v':                                                                             // dump AST after decl validation pass
[b87a5ed]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;
[7b937575]179                  case 'F':                                                                             // source file-name without suffix
180                        filename = optarg;
181                        break;
[b87a5ed]182                  case '?':
183                        cout << "Unknown option: '" << (char)optopt << "'" << endl;
[dd51906]184                        exit( EXIT_FAILURE );
[b87a5ed]185                  default:
186                        abort();
187                } // switch
188        } // while
189
190        try {
[81419b5]191                // choose to read the program from a file or stdin
[b87a5ed]192                if ( optind < argc ) {
193                        input = fopen( argv[ optind ], "r" );
194                        if ( ! input ) {
[711eee5]195                                std::cout << "Error: can't open " << argv[ optind ] << std::endl;
[dd51906]196                                exit( EXIT_FAILURE );
[b87a5ed]197                        } // if
[d029162]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 ];
[37024fd]200                        // prelude filename comes in differently
201                        if ( libcfap ) filename = "prelude.cf";
[b87a5ed]202                        optind += 1;
203                } else {
204                        input = stdin;
[2a7e29b]205                        // if running cfa-cpp directly, might forget to pass -F option. Since this takes from stdin, pass
206                        // a fake name along
207                        if ( filename == NULL ) filename = "stdin";
[b87a5ed]208                } // if
209
210                if ( optind < argc ) {
211                        output = new ofstream( argv[ optind ] );
212                } // if
[71f4e4f]213
[b87a5ed]214                Parser::get_parser().set_debug( grammarp );
[81419b5]215
[159c62e]216                // read in the builtins, extras, and the prelude
[de62360d]217                if ( ! nopreludep ) {                                                   // include gcc builtins
[faf8857]218                        // -l is for initial build ONLY and builtins.cf is not in the lib directory so access it here.
[d3b7937]219                        FILE * builtins = fopen( libcfap | treep ? "builtins.cf" : CFA_LIBDIR "/builtins.cf", "r" );
[b87a5ed]220                        if ( builtins == NULL ) {
[d3b7937]221                                std::cerr << "Error: can't open builtins.cf" << std::endl;
[dd51906]222                                exit( EXIT_FAILURE );
[b87a5ed]223                        } // if
[81419b5]224                        parse( builtins, LinkageSpec::Compiler );
225
[159c62e]226                        // read the extra prelude in, if not generating the cfa library
227                        FILE * extras = fopen( libcfap | treep ? "extras.cf" : CFA_LIBDIR "/extras.cf", "r" );
228                        if ( extras == NULL ) {
229                                std::cerr << "Error: can't open extras.cf" << std::endl;
230                                exit( EXIT_FAILURE );
231                        } // if
232                        parse( extras, LinkageSpec::C );
233
[81419b5]234                        if ( ! libcfap ) {
[faf8857]235                                // read the prelude in, if not generating the cfa library
[d3b7937]236                                FILE * prelude = fopen( treep ? "prelude.cf" : CFA_LIBDIR "/prelude.cf", "r" );
[81419b5]237                                if ( prelude == NULL ) {
[d3b7937]238                                        std::cerr << "Error: can't open prelude.cf" << std::endl;
[dd51906]239                                        exit( EXIT_FAILURE );
[81419b5]240                                } // if
[71f4e4f]241
[35304009]242                                parse( prelude, LinkageSpec::Intrinsic );
[b87a5ed]243                        } // if
244                } // if
[81419b5]245
[71f4e4f]246                parse( input, libcfap ? LinkageSpec::Intrinsic : LinkageSpec::Cforall, grammarp );
247
[b87a5ed]248                if ( parsep ) {
249                        Parser::get_parser().get_parseTree()->printList( std::cout );
250                        Parser::get_parser().freeTree();
251                        return 0;
252                } // if
253
254                buildList( Parser::get_parser().get_parseTree(), translationUnit );
255
256                Parser::get_parser().freeTree();
257                if ( astp ) {
[1ab4ce2]258                        dump( translationUnit );
[b87a5ed]259                        return 0;
260                } // if
261
[839ccbb]262                // add the assignment statement after the initialization of a type parameter
[81419b5]263                OPTPRINT( "validate" )
264                SymTab::validate( translationUnit, symtabp );
265                if ( symtabp ) {
[b87a5ed]266                        return 0;
267                } // if
268
[81419b5]269                if ( expraltp ) {
270                        ResolvExpr::AlternativePrinter printer( std::cout );
271                        acceptAll( translationUnit, printer );
[b87a5ed]272                        return 0;
273                } // if
274
275                if ( validp ) {
[1ab4ce2]276                        dump( translationUnit );
[b87a5ed]277                        return 0;
278                } // if
279
[81419b5]280                OPTPRINT( "mutate" )
281                ControlStruct::mutate( translationUnit );
[71f4e4f]282                OPTPRINT( "fixNames" )
[81419b5]283                CodeGen::fixNames( translationUnit );
[711eee5]284                OPTPRINT( "fixGlobalInit" );
[03e5d14]285                InitTweak::fixGlobalInit( translationUnit, filename, libcfap || treep );
[f1e012b]286                OPTPRINT( "tweakInit" )
[a0fdbd5]287                InitTweak::genInit( translationUnit );
[b87a5ed]288
[81419b5]289                if ( libcfap ) {
290                        // generate the bodies of cfa library functions
291                        LibCfa::makeLibCfa( translationUnit );
[b87a5ed]292                } // if
293
[de62360d]294                if ( bresolvep ) {
[1ab4ce2]295                        dump( translationUnit );
[de62360d]296                        return 0;
297                } // if
298
[81419b5]299                OPTPRINT( "resolve" )
[b87a5ed]300                ResolvExpr::resolve( translationUnit );
[81419b5]301                if ( exprp ) {
[1ab4ce2]302                        dump( translationUnit );
[ca1c11f]303                        return 0;
[81419b5]304                }
305
[f1e012b]306                OPTPRINT( "fixInit" )
[71f4e4f]307                // fix ObjectDecl - replaces ConstructorInit nodes
308                InitTweak::fix( translationUnit );
[ca1c11f]309                if ( ctorinitp ) {
310                        dump ( translationUnit );
311                        return 0;
312                }
[71f4e4f]313
[81419b5]314                OPTPRINT( "copyParams" );
[b87a5ed]315                GenPoly::copyParams( translationUnit );
[81419b5]316                OPTPRINT( "convertSpecializations" )
[698664b3]317                GenPoly::convertSpecializations( translationUnit );
[81419b5]318                OPTPRINT( "convertLvalue" )
[b87a5ed]319                GenPoly::convertLvalue( translationUnit );
[fea7ca7]320
321                if ( bboxp ) {
322                        dump( translationUnit );
323                        return 0;
324                }
[81419b5]325                OPTPRINT( "box" )
[b87a5ed]326                GenPoly::box( translationUnit );
[81419b5]327
[2871210]328                // print tree right before code generation
[81419b5]329                if ( codegenp ) {
[1ab4ce2]330                        dump( translationUnit );
[81419b5]331                        return 0;
332                } // if
[b87a5ed]333
[1ab4ce2]334                CodeGen::generate( translationUnit, *output, ! noprotop );
[b87a5ed]335
336                if ( output != &std::cout ) {
337                        delete output;
338                } // if
339        } catch ( SemanticError &e ) {
340                if ( errorp ) {
[f77f12e2]341                        std::cerr << "---AST at error:---" << std::endl;
342                        dump( translationUnit, std::cerr );
343                        std::cerr << std::endl << "---End of AST, begin error message:---\n" << std::endl;
[b87a5ed]344                }
[81419b5]345                e.print( std::cerr );
[b87a5ed]346                if ( output != &std::cout ) {
347                        delete output;
348                } // if
349                return 1;
350        } catch ( UnimplementedError &e ) {
351                std::cout << "Sorry, " << e.get_what() << " is not currently implemented" << std::endl;
352                if ( output != &std::cout ) {
353                        delete output;
354                } // if
355                return 1;
356        } catch ( CompilerError &e ) {
357                std::cerr << "Compiler Error: " << e.get_what() << std::endl;
358                std::cerr << "(please report bugs to " << std::endl;
359                if ( output != &std::cout ) {
360                        delete output;
361                } // if
362                return 1;
363        } // try
364
[39786813]365        deleteAll( translationUnit );
[b87a5ed]366        return 0;
[d9a0e76]367} // main
[51b7345]368
[1ab4ce2]369static void parse( FILE * input, LinkageSpec::Type linkage, bool shouldExit ) {
[81419b5]370        Parser::get_parser().set_linkage( linkage );
371        Parser::get_parser().parse( input );
372
373        fclose( input );
374        if ( shouldExit || Parser::get_parser().get_parseStatus() != 0 ) {
375                exit( Parser::get_parser().get_parseStatus() );
376        } // if
377}
378
[1ab4ce2]379static bool notPrelude( Declaration * decl ) {
380        return ! LinkageSpec::isBuiltin( decl->get_linkage() );
381}
382
[f77f12e2]383static void dump( std::list< Declaration * > & translationUnit, std::ostream & out ) {
[1ab4ce2]384        std::list< Declaration * > decls;
385        if ( noprotop ) {
[71f4e4f]386                filter( translationUnit.begin(), translationUnit.end(),
[1ab4ce2]387                                std::back_inserter( decls ), notPrelude );
388        } else {
389                decls = translationUnit;
390        }
391
[f77f12e2]392        printAll( decls, out );
[7f5566b]393        deleteAll( translationUnit );
[1ab4ce2]394}
395
396
[51b7345]397// Local Variables: //
[b87a5ed]398// tab-width: 4 //
399// mode: c++ //
400// compile-command: "make install" //
[51b7345]401// End:  //
Note: See TracBrowser for help on using the repository browser.