source: src/main.cc @ 7a560c1

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 7a560c1 was fa2de95, checked in by Aaron Moss <a3moss@…>, 7 years ago

Initial functional version of DeclStats?

  • Property mode set to 100644
File size: 14.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
[375a068]12// Last Modified On : Wed Dec 14 14:35:54 2016
13// Update Count     : 436
[b87a5ed]14//
15
[51b7345]16#include <iostream>
17#include <fstream>
[e6955b1]18#include <signal.h>                                                                             // signal
19#include <getopt.h>                                                                             // getopt
[0afffee]20#include <execinfo.h>                                                                   // backtrace, backtrace_symbols
[e6955b1]21#include <cxxabi.h>                                                                             // __cxa_demangle
[0afffee]22#include <cstring>                                                                              // index
[e6955b1]23
24using namespace std;
25
[0da3e2c]26#include "Parser/lex.h"
27#include "Parser/parser.h"
28#include "Parser/TypedefTable.h"
[51b7345]29#include "GenPoly/Lvalue.h"
30#include "GenPoly/Specialize.h"
31#include "GenPoly/Box.h"
32#include "GenPoly/CopyParams.h"
[ea5daeb]33#include "GenPoly/InstantiateGeneric.h"
[51b7345]34#include "CodeGen/Generate.h"
35#include "CodeGen/FixNames.h"
[13de47bc]36#include "CodeGen/FixMain.h"
[41a7137]37#include "CodeTools/DeclStats.h"
[51b7345]38#include "ControlStruct/Mutate.h"
39#include "SymTab/Validate.h"
40#include "ResolvExpr/AlternativePrinter.h"
41#include "ResolvExpr/Resolver.h"
42#include "MakeLibCfa.h"
[a0fdbd5]43#include "InitTweak/GenInit.h"
[71f4e4f]44#include "InitTweak/FixInit.h"
[d3b7937]45#include "Common/UnimplementedError.h"
[51b7345]46#include "../config.h"
[6eb8948]47#include "Tuples/Tuples.h"
[51b7345]48
49using namespace std;
50
[e6955b1]51#define OPTPRINT(x) if ( errorp ) cerr << x << endl;
[81419b5]52
[0da3e2c]53
[8b7ee09]54LinkageSpec::Spec linkage = LinkageSpec::Cforall;
[0da3e2c]55TypedefTable typedefTable;
[cbaee0d]56DeclarationNode * parseTree = nullptr;                                  // program parse tree
[81419b5]57
[926af74]58extern int yydebug;                                                                             // set for -g flag (Grammar)
[b87a5ed]59bool
60        astp = false,
[de62360d]61        bresolvep = false,
[fea7ca7]62        bboxp = false,
[ca1c11f]63        ctorinitp = false,
[41a7137]64        declstatsp = false,
[b87a5ed]65        exprp = false,
66        expraltp = false,
67        libcfap = false,
[de62360d]68        nopreludep = false,
[1ab4ce2]69        noprotop = false,
[3fe34ae]70        nomainp = false,
[de62360d]71        parsep = false,
[b87a5ed]72        resolvep = false,                                                                       // used in AlternativeFinder
73        symtabp = false,
[d3b7937]74        treep = false,
[626dbc10]75        tuplep = false,
[b87a5ed]76        validp = false,
[de62360d]77        errorp = false,
78        codegenp = false;
[b87a5ed]79
[926af74]80static void parse_cmdline( int argc, char *argv[], const char *& filename );
[8b7ee09]81static void parse( FILE * input, LinkageSpec::Spec linkage, bool shouldExit = false );
[e6955b1]82static void dump( list< Declaration * > & translationUnit, ostream & out = cout );
83
[0afffee]84static void backtrace( int start ) {                                    // skip first N stack frames
[e6955b1]85        enum { Frames = 50 };
86        void * array[Frames];
[0afffee]87        int size = ::backtrace( array, Frames );
88        char ** messages = ::backtrace_symbols( array, size ); // does not demangle names
89
90        *index( messages[0], '(' ) = '\0';                                      // find executable name
91        cerr << "Stack back trace for: " << messages[0] << endl;
[e6955b1]92
[b542bfb]93        // skip last 2 stack frames after main
94        for ( int i = start; i < size - 2 && messages != nullptr; i += 1 ) {
[e6955b1]95                char * mangled_name = nullptr, * offset_begin = nullptr, * offset_end = nullptr;
96                for ( char *p = messages[i]; *p; ++p ) {        // find parantheses and +offset
[0afffee]97                        if ( *p == '(' ) {
[46f6134]98                                mangled_name = p;
[0afffee]99                        } else if ( *p == '+' ) {
[e6955b1]100                                offset_begin = p;
[0afffee]101                        } else if ( *p == ')' ) {
[e6955b1]102                                offset_end = p;
103                                break;
104                        } // if
105                } // for
106
107                // if line contains symbol, attempt to demangle
[b542bfb]108                int frameNo = i - start;
[e6955b1]109                if ( mangled_name && offset_begin && offset_end && mangled_name < offset_begin ) {
[0afffee]110                        *mangled_name++ = '\0';                                         // delimit strings
[e6955b1]111                        *offset_begin++ = '\0';
112                        *offset_end++ = '\0';
113
[0afffee]114                        int status;
[e6955b1]115                        char * real_name = __cxxabiv1::__cxa_demangle( mangled_name, 0, 0, &status );
[0afffee]116                        // bug in __cxa_demangle for single-character lower-case non-mangled names
[e6955b1]117                        if ( status == 0 ) {                                            // demangling successful ?
[b542bfb]118                                cerr << "(" << frameNo << ") " << messages[i] << " : "
[e6955b1]119                                         << real_name << "+" << offset_begin << offset_end << endl;
120                        } else {                                                                        // otherwise, output mangled name
[b542bfb]121                                cerr << "(" << frameNo << ") " << messages[i] << " : "
[0afffee]122                                         << mangled_name << "(/*unknown*/)+" << offset_begin << offset_end << endl;
[e6955b1]123                        } // if
[0afffee]124
[e6955b1]125                        free( real_name );
126                } else {                                                                                // otherwise, print the whole line
[b542bfb]127                        cerr << "(" << frameNo << ") " << messages[i] << endl;
[e6955b1]128                } // if
129        } // for
[b542bfb]130
[e6955b1]131        free( messages );
[b542bfb]132} // backtrace
133
134void sigSegvBusHandler( int sig_num ) {
135        cerr << "*CFA runtime error* program cfa-cpp terminated with "
136                 <<     (sig_num == SIGSEGV ? "segment fault" : "bus error")
[0afffee]137                 << "." << endl;
[b542bfb]138        backtrace( 2 );                                                                         // skip first 2 stack frames
[e6955b1]139        exit( EXIT_FAILURE );
140} // sigSegvBusHandler
[0da3e2c]141
[b542bfb]142void sigAbortHandler( int sig_num ) {
143        backtrace( 6 );                                                                         // skip first 6 stack frames
144        signal( SIGABRT, SIG_DFL);                                                      // reset default signal handler
145    raise( SIGABRT );                                                                   // reraise SIGABRT
146} // sigAbortHandler
147
148
[cbaee0d]149int main( int argc, char * argv[] ) {
[3b8e52c]150        FILE * input;                                                                           // use FILE rather than istream because yyin is FILE
[e6955b1]151        ostream *output = & cout;
[926af74]152        const char *filename = nullptr;
[e6955b1]153        list< Declaration * > translationUnit;
154
155        signal( SIGSEGV, sigSegvBusHandler );
156        signal( SIGBUS, sigSegvBusHandler );
[b542bfb]157        signal( SIGABRT, sigAbortHandler );
[b87a5ed]158
[0da3e2c]159        parse_cmdline( argc, argv, filename );                          // process command-line arguments
[13de47bc]160        CodeGen::FixMain::setReplaceMain( !nomainp );
[b87a5ed]161
162        try {
[81419b5]163                // choose to read the program from a file or stdin
[3b8e52c]164                if ( optind < argc ) {                                                  // any commands after the flags ? => input file name
[b87a5ed]165                        input = fopen( argv[ optind ], "r" );
[3b8e52c]166                        assertf( input, "cannot open %s\n", argv[ optind ] );
[d029162]167                        // if running cfa-cpp directly, might forget to pass -F option (and really shouldn't have to)
[926af74]168                        if ( filename == nullptr ) filename = argv[ optind ];
[37024fd]169                        // prelude filename comes in differently
170                        if ( libcfap ) filename = "prelude.cf";
[b87a5ed]171                        optind += 1;
[3b8e52c]172                } else {                                                                                // no input file name
[b87a5ed]173                        input = stdin;
[2a7e29b]174                        // if running cfa-cpp directly, might forget to pass -F option. Since this takes from stdin, pass
175                        // a fake name along
[926af74]176                        if ( filename == nullptr ) filename = "stdin";
[b87a5ed]177                } // if
178
[159c62e]179                // read in the builtins, extras, and the prelude
[de62360d]180                if ( ! nopreludep ) {                                                   // include gcc builtins
[faf8857]181                        // -l is for initial build ONLY and builtins.cf is not in the lib directory so access it here.
[375a068]182                        FILE * builtins = fopen( libcfap | treep ? "../prelude/builtins.cf" : CFA_LIBDIR "/builtins.cf", "r" );
[3b8e52c]183                        assertf( builtins, "cannot open builtins.cf\n" );
[81419b5]184                        parse( builtins, LinkageSpec::Compiler );
185
[159c62e]186                        // read the extra prelude in, if not generating the cfa library
[375a068]187                        FILE * extras = fopen( libcfap | treep ? "../prelude/extras.cf" : CFA_LIBDIR "/extras.cf", "r" );
[3b8e52c]188                        assertf( extras, "cannot open extras.cf\n" );
[159c62e]189                        parse( extras, LinkageSpec::C );
190
[81419b5]191                        if ( ! libcfap ) {
[faf8857]192                                // read the prelude in, if not generating the cfa library
[375a068]193                                FILE * prelude = fopen( treep ? "../prelude/prelude.cf" : CFA_LIBDIR "/prelude.cf", "r" );
[3b8e52c]194                                assertf( prelude, "cannot open prelude.cf\n" );
[35304009]195                                parse( prelude, LinkageSpec::Intrinsic );
[b87a5ed]196                        } // if
197                } // if
[81419b5]198
[926af74]199                parse( input, libcfap ? LinkageSpec::Intrinsic : LinkageSpec::Cforall, yydebug );
[71f4e4f]200
[b87a5ed]201                if ( parsep ) {
[e6955b1]202                        parseTree->printList( cout );
[0da3e2c]203                        delete parseTree;
[b87a5ed]204                        return 0;
205                } // if
206
[0da3e2c]207                buildList( parseTree, translationUnit );
208                delete parseTree;
[cbaee0d]209                parseTree = nullptr;
[b87a5ed]210
211                if ( astp ) {
[1ab4ce2]212                        dump( translationUnit );
[b87a5ed]213                        return 0;
214                } // if
215
[839ccbb]216                // add the assignment statement after the initialization of a type parameter
[81419b5]217                OPTPRINT( "validate" )
218                SymTab::validate( translationUnit, symtabp );
219                if ( symtabp ) {
[46f6134]220                        deleteAll( translationUnit );
[b87a5ed]221                        return 0;
222                } // if
223
[81419b5]224                if ( expraltp ) {
[e6955b1]225                        ResolvExpr::AlternativePrinter printer( cout );
[81419b5]226                        acceptAll( translationUnit, printer );
[b87a5ed]227                        return 0;
228                } // if
229
230                if ( validp ) {
[1ab4ce2]231                        dump( translationUnit );
[b87a5ed]232                        return 0;
233                } // if
234
[81419b5]235                OPTPRINT( "mutate" )
236                ControlStruct::mutate( translationUnit );
[71f4e4f]237                OPTPRINT( "fixNames" )
[81419b5]238                CodeGen::fixNames( translationUnit );
[f1e012b]239                OPTPRINT( "tweakInit" )
[a0fdbd5]240                InitTweak::genInit( translationUnit );
[bf32bb8]241                OPTPRINT( "expandMemberTuples" );
242                Tuples::expandMemberTuples( translationUnit );
[81419b5]243                if ( libcfap ) {
244                        // generate the bodies of cfa library functions
245                        LibCfa::makeLibCfa( translationUnit );
[b87a5ed]246                } // if
247
[fa2de95]248                if ( declstatsp ) {
249                        CodeTools::printDeclStats( translationUnit );
250                        deleteAll( translationUnit );
251                        return 0;
252                }
253
[de62360d]254                if ( bresolvep ) {
[1ab4ce2]255                        dump( translationUnit );
[de62360d]256                        return 0;
257                } // if
258
[81419b5]259                OPTPRINT( "resolve" )
[b87a5ed]260                ResolvExpr::resolve( translationUnit );
[81419b5]261                if ( exprp ) {
[1ab4ce2]262                        dump( translationUnit );
[ca1c11f]263                        return 0;
[926af74]264                } // if
[81419b5]265
[71f4e4f]266                // fix ObjectDecl - replaces ConstructorInit nodes
[6cf27a07]267                OPTPRINT( "fixInit" )
268                InitTweak::fix( translationUnit, filename, libcfap || treep );
[ca1c11f]269                if ( ctorinitp ) {
270                        dump ( translationUnit );
271                        return 0;
[926af74]272                } // if
[71f4e4f]273
[141b786]274                OPTPRINT( "expandUniqueExpr" ); // xxx - is this the right place for this? want to expand ASAP so that subsequent passes don't need to worry about double-visiting a unique expr - needs to go after InitTweak::fix so that copy constructed return declarations are reused
275                Tuples::expandUniqueExpr( translationUnit );
[626dbc10]276
277                OPTPRINT( "convertSpecializations" ) // needs to happen before tuple types are expanded
278                GenPoly::convertSpecializations( translationUnit );
279
[d9fa60a]280                OPTPRINT( "expandTuples" ); // xxx - is this the right place for this?
281                Tuples::expandTuples( translationUnit );
[626dbc10]282                if ( tuplep ) {
283                        dump( translationUnit );
284                        return 0;
285                }
[141b786]286
[ea5daeb]287                OPTPRINT("instantiateGenerics")
288                GenPoly::instantiateGeneric( translationUnit );
[81419b5]289                OPTPRINT( "copyParams" );
[b87a5ed]290                GenPoly::copyParams( translationUnit );
[81419b5]291                OPTPRINT( "convertLvalue" )
[b87a5ed]292                GenPoly::convertLvalue( translationUnit );
[fea7ca7]293
294                if ( bboxp ) {
295                        dump( translationUnit );
296                        return 0;
[926af74]297                } // if
[81419b5]298                OPTPRINT( "box" )
[b87a5ed]299                GenPoly::box( translationUnit );
[81419b5]300
[2871210]301                // print tree right before code generation
[81419b5]302                if ( codegenp ) {
[1ab4ce2]303                        dump( translationUnit );
[81419b5]304                        return 0;
305                } // if
[b87a5ed]306
[13de47bc]307                if ( optind < argc ) {                                                  // any commands after the flags and input file ? => output file name
308                        output = new ofstream( argv[ optind ] );
309                } // if
[0270824]310
[13de47bc]311                CodeGen::generate( translationUnit, *output, ! noprotop );
[0270824]312
[13de47bc]313                CodeGen::FixMain::fix( *output, treep ? "../prelude/bootloader.c" : CFA_LIBDIR "/bootloader.c" );
[0270824]314
[e6955b1]315                if ( output != &cout ) {
[b87a5ed]316                        delete output;
317                } // if
318        } catch ( SemanticError &e ) {
319                if ( errorp ) {
[e6955b1]320                        cerr << "---AST at error:---" << endl;
321                        dump( translationUnit, cerr );
322                        cerr << endl << "---End of AST, begin error message:---\n" << endl;
[926af74]323                } // if
[e6955b1]324                e.print( cerr );
325                if ( output != &cout ) {
[b87a5ed]326                        delete output;
327                } // if
328                return 1;
329        } catch ( UnimplementedError &e ) {
[e6955b1]330                cout << "Sorry, " << e.get_what() << " is not currently implemented" << endl;
331                if ( output != &cout ) {
[b87a5ed]332                        delete output;
333                } // if
334                return 1;
335        } catch ( CompilerError &e ) {
[e6955b1]336                cerr << "Compiler Error: " << e.get_what() << endl;
337                cerr << "(please report bugs to " << endl;
338                if ( output != &cout ) {
[b87a5ed]339                        delete output;
340                } // if
341                return 1;
342        } // try
343
[39786813]344        deleteAll( translationUnit );
[b87a5ed]345        return 0;
[d9a0e76]346} // main
[51b7345]347
[cbaee0d]348void parse_cmdline( int argc, char * argv[], const char *& filename ) {
[41a7137]349        enum { Ast, Bbox, Bresolver, CtorInitFix, DeclStats, Expr, ExprAlt, Grammar, LibCFA, Nopreamble, Parse, Prototypes, Resolver, Symbol, Tree, TupleExpansion, Validate, };
[0da3e2c]350
351        static struct option long_opts[] = {
352                { "ast", no_argument, 0, Ast },
353                { "before-box", no_argument, 0, Bbox },
354                { "before-resolver", no_argument, 0, Bresolver },
355                { "ctorinitfix", no_argument, 0, CtorInitFix },
[41a7137]356                { "decl-stats", no_argument, 0, DeclStats },
[0da3e2c]357                { "expr", no_argument, 0, Expr },
358                { "expralt", no_argument, 0, ExprAlt },
359                { "grammar", no_argument, 0, Grammar },
360                { "libcfa", no_argument, 0, LibCFA },
361                { "no-preamble", no_argument, 0, Nopreamble },
362                { "parse", no_argument, 0, Parse },
363                { "no-prototypes", no_argument, 0, Prototypes },
364                { "resolver", no_argument, 0, Resolver },
365                { "symbol", no_argument, 0, Symbol },
366                { "tree", no_argument, 0, Tree },
[626dbc10]367                { "tuple-expansion", no_argument, 0, TupleExpansion },
[0da3e2c]368                { "validate", no_argument, 0, Validate },
369                { 0, 0, 0, 0 }
370        }; // long_opts
371        int long_index;
372
373        opterr = 0;                                                                                     // (global) prevent getopt from printing error messages
374
375        int c;
[41a7137]376        while ( (c = getopt_long( argc, argv, "abBcdefglmnpqrstTvyzD:F:", long_opts, &long_index )) != -1 ) {
[0da3e2c]377                switch ( c ) {
378                  case Ast:
379                  case 'a':                                                                             // dump AST
380                        astp = true;
381                        break;
382                  case Bresolver:
383                  case 'b':                                                                             // print before resolver steps
384                        bresolvep = true;
385                        break;
[626dbc10]386                  case 'B':                                                                             // print before box steps
[0da3e2c]387                        bboxp = true;
388                        break;
389                  case CtorInitFix:
390                  case 'c':
391                        ctorinitp = true;
392                        break;
[41a7137]393                  case DeclStats:
394                  case 'd':
395                    declstatsp = true;
396                        break;
[0da3e2c]397                  case Expr:
398                  case 'e':                                                                             // dump AST after expression analysis
399                        exprp = true;
400                        break;
401                  case ExprAlt:
402                  case 'f':                                                                             // print alternatives for expressions
403                        expraltp = true;
404                        break;
405                  case Grammar:
406                  case 'g':                                                                             // bison debugging info (grammar rules)
[926af74]407                        yydebug = true;
[0da3e2c]408                        break;
409                  case LibCFA:
410                  case 'l':                                                                             // generate libcfa.c
411                        libcfap = true;
412                        break;
413                  case Nopreamble:
414                  case 'n':                                                                             // do not read preamble
415                        nopreludep = true;
416                        break;
417                  case Prototypes:
418                  case 'p':                                                                             // generate prototypes for preamble functions
419                        noprotop = true;
420                        break;
[3fe34ae]421                  case 'm':                                                                             // don't replace the main
422                        nomainp = true;
423                        break;
[0da3e2c]424                  case Parse:
425                  case 'q':                                                                             // dump parse tree
426                        parsep = true;
427                        break;
428                  case Resolver:
429                  case 'r':                                                                             // print resolver steps
430                        resolvep = true;
431                        break;
432                  case Symbol:
433                  case 's':                                                                             // print symbol table events
434                        symtabp = true;
435                        break;
436                  case Tree:
437                  case 't':                                                                             // build in tree
438                        treep = true;
439                        break;
[626dbc10]440                  case TupleExpansion:
441                  case 'T':                                                                             // print after tuple expansion
442                        tuplep = true;
443                        break;
[0da3e2c]444                  case 'v':                                                                             // dump AST after decl validation pass
445                        validp = true;
446                        break;
447                  case 'y':
448                        errorp = true;
449                        break;
450                  case 'z':
451                        codegenp = true;
452                        break;
453                  case 'D':                                                                             // ignore -Dxxx
454                        break;
455                  case 'F':                                                                             // source file-name without suffix
456                        filename = optarg;
457                        break;
458                  case '?':
[3b8e52c]459                        assertf( false, "Unknown option: '%c'\n", (char)optopt );
[0da3e2c]460                  default:
461                        abort();
462                } // switch
463        } // while
464} // parse_cmdline
465
[8b7ee09]466static void parse( FILE * input, LinkageSpec::Spec linkage, bool shouldExit ) {
[0da3e2c]467        extern int yyparse( void );
[cbaee0d]468        extern FILE * yyin;
[0da3e2c]469        extern int yylineno;
470
[8b7ee09]471        ::linkage = linkage;                                                            // set globals
[0da3e2c]472        yyin = input;
473        yylineno = 1;
474        typedefTable.enterScope();
475        int parseStatus = yyparse();
[81419b5]476
477        fclose( input );
[0da3e2c]478        if ( shouldExit || parseStatus != 0 ) {
479                exit( parseStatus );
[81419b5]480        } // if
[0da3e2c]481} // parse
[81419b5]482
[1ab4ce2]483static bool notPrelude( Declaration * decl ) {
484        return ! LinkageSpec::isBuiltin( decl->get_linkage() );
[0da3e2c]485} // notPrelude
[1ab4ce2]486
[e6955b1]487static void dump( list< Declaration * > & translationUnit, ostream & out ) {
488        list< Declaration * > decls;
[926af74]489
[1ab4ce2]490        if ( noprotop ) {
[e6955b1]491                filter( translationUnit.begin(), translationUnit.end(), back_inserter( decls ), notPrelude );
[1ab4ce2]492        } else {
493                decls = translationUnit;
[926af74]494        } // if
[1ab4ce2]495
[f77f12e2]496        printAll( decls, out );
[7f5566b]497        deleteAll( translationUnit );
[0da3e2c]498} // dump
[1ab4ce2]499
[51b7345]500// Local Variables: //
[b87a5ed]501// tab-width: 4 //
502// mode: c++ //
503// compile-command: "make install" //
[51b7345]504// End:  //
Note: See TracBrowser for help on using the repository browser.