source: src/main.cc @ 066d77a

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

refactor genCtorInit, generate ConstructorInit? for UniqueExpr?

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