source: src/main.cc @ d1ab5331

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

Account for lvalue returning functions in FixCopyCtor?, removed ConditionalExpr?, CommaExpr?, Logical Expr mutates from Specialize, added before box debug flag

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