source: src/main.cc @ 84118d8

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 84118d8 was 0afffee, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

update stack trace on cfa error

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