source: src/main.cc @ 627f585

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 627f585 was 626dbc10, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

major refactoring of specialization code, added code to generate thunks for ttype functions, move specialize pass to before tuple expansion

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