Changeset 3d5701e for src/main.cc


Ignore:
Timestamp:
Feb 25, 2020, 1:17:33 PM (6 years ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
7dc2e015
Parents:
9fb8f01 (diff), dd9e1ca (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

resolve conflict

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/main.cc

    r9fb8f01 r3d5701e  
    1010// Created On       : Fri May 15 23:12:02 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Aug 23 06:50:08 2019
    13 // Update Count     : 607
     12// Last Modified On : Sat Feb  8 08:33:50 2020
     13// Update Count     : 633
    1414//
    1515
     
    2020#include <cstdio>                           // for fopen, FILE, fclose, stdin
    2121#include <cstdlib>                          // for exit, free, abort, EXIT_F...
    22 #include <csignal>                         // for signal, SIGABRT, SIGSEGV
     22#include <csignal>                          // for signal, SIGABRT, SIGSEGV
    2323#include <cstring>                          // for index
    2424#include <fstream>                          // for ofstream
     
    2828#include <list>                             // for list
    2929#include <string>                           // for char_traits, operator<<
     30
     31using namespace std;
     32
    3033
    3134#include "CompilationState.h"
     
    5356#include "InitTweak/GenInit.h"              // for genInit
    5457#include "MakeLibCfa.h"                     // for makeLibCfa
    55 #include "Parser/LinkageSpec.h"             // for Spec, Cforall, Intrinsic
    5658#include "Parser/ParseNode.h"               // for DeclarationNode, buildList
    5759#include "Parser/TypedefTable.h"            // for TypedefTable
     
    5961#include "ResolvExpr/Resolver.h"            // for resolve
    6062#include "SymTab/Validate.h"                // for validate
    61 #include "SynTree/TopLvalue.h"              // for assertTopLvalue, clearInn...
     63#include "SynTree/LinkageSpec.h"            // for Spec, Cforall, Intrinsic
    6264#include "SynTree/Declaration.h"            // for Declaration
    6365#include "SynTree/Visitor.h"                // for acceptAll
     
    6567#include "Virtual/ExpandCasts.h"            // for expandCasts
    6668
    67 
    68 using namespace std;
    6969
    7070static void NewPass( const char * const name ) {
     
    9898static bool waiting_for_gdb = false;                                    // flag to set cfa-cpp to wait for gdb on start
    9999
    100 static std::string PreludeDirector = "";
     100static string PreludeDirector = "";
    101101
    102102static void parse_cmdline( int argc, char *argv[] );
     
    105105
    106106static void backtrace( int start ) {                                    // skip first N stack frames
    107         enum { Frames = 50 };
     107        enum { Frames = 50, };                                                          // maximum number of stack frames
    108108        void * array[Frames];
    109         int size = ::backtrace( array, Frames );
     109        size_t size = ::backtrace( array, Frames );
    110110        char ** messages = ::backtrace_symbols( array, size ); // does not demangle names
    111111
     
    114114
    115115        // skip last 2 stack frames after main
    116         for ( int i = start; i < size - 2 && messages != nullptr; i += 1 ) {
     116        for ( unsigned int i = start; i < size - 2 && messages != nullptr; i += 1 ) {
    117117                char * mangled_name = nullptr, * offset_begin = nullptr, * offset_end = nullptr;
    118                 for ( char *p = messages[i]; *p; ++p ) {        // find parantheses and +offset
     118
     119                for ( char * p = messages[i]; *p; p += 1 ) {    // find parantheses and +offset
    119120                        if ( *p == '(' ) {
    120121                                mangled_name = p;
     
    154155} // backtrace
    155156
    156 static void sigSegvBusHandler( int sig_num ) {
    157         cerr << "*CFA runtime error* program cfa-cpp terminated with "
    158                  <<     (sig_num == SIGSEGV ? "segment fault" : "bus error")
    159                  << "." << endl;
     157#define SIGPARMS int sig __attribute__(( unused )), siginfo_t * sfp __attribute__(( unused )), ucontext_t * cxt __attribute__(( unused ))
     158
     159static void Signal( int sig, void (*handler)(SIGPARMS), int flags ) {
     160        struct sigaction act;
     161
     162        act.sa_sigaction = (void (*)(int, siginfo_t *, void *))handler;
     163        act.sa_flags = flags;
     164
     165        if ( sigaction( sig, &act, nullptr ) == -1 ) {
     166            cerr << "*CFA runtime error* problem installing signal handler, error(" << errno << ") " << strerror( errno ) << endl;
     167            _exit( EXIT_FAILURE );
     168        } // if
     169} // Signal
     170
     171static void sigSegvBusHandler( SIGPARMS ) {
     172        if ( sfp->si_addr == nullptr ) {
     173                cerr << "Null pointer (nullptr) dereference." << endl;
     174        } else {
     175                cerr << (sig == SIGSEGV ? "Segment fault" : "Bus error") << " at memory location " << sfp->si_addr << "." << endl
     176                         << "Possible cause is reading outside the address space or writing to a protected area within the address space with an invalid pointer or subscript." << endl;
     177        } // if
    160178        backtrace( 2 );                                                                         // skip first 2 stack frames
    161         //_exit( EXIT_FAILURE );
    162179        abort();                                                                                        // cause core dump for debugging
    163180} // sigSegvBusHandler
    164181
    165 static void sigAbortHandler( __attribute__((unused)) int sig_num ) {
     182static void sigFpeHandler( SIGPARMS ) {
     183        const char * msg;
     184
     185        switch ( sfp->si_code ) {
     186          case FPE_INTDIV: case FPE_FLTDIV: msg = "divide by zero"; break;
     187          case FPE_FLTOVF: msg = "overflow"; break;
     188          case FPE_FLTUND: msg = "underflow"; break;
     189          case FPE_FLTRES: msg = "inexact result"; break;
     190          case FPE_FLTINV: msg = "invalid operation"; break;
     191          default: msg = "unknown";
     192        } // choose
     193        cerr << "Computation error " << msg << " at location " << sfp->si_addr << endl
     194                 << "Possible cause is constant-expression evaluation invalid." << endl;
     195        backtrace( 2 );                                                                         // skip first 2 stack frames
     196        abort();                                                                                        // cause core dump for debugging
     197} // sigFpeHandler
     198
     199static void sigAbortHandler( SIGPARMS ) {
    166200        backtrace( 6 );                                                                         // skip first 6 stack frames
    167         signal( SIGABRT, SIG_DFL);                                                      // reset default signal handler
     201        Signal( SIGABRT, (void (*)(SIGPARMS))SIG_DFL, SA_SIGINFO );     // reset default signal handler
    168202        raise( SIGABRT );                                                                       // reraise SIGABRT
    169203} // sigAbortHandler
     
    174208        list< Declaration * > translationUnit;
    175209
    176         signal( SIGSEGV, sigSegvBusHandler );
    177         signal( SIGBUS, sigSegvBusHandler );
    178         signal( SIGABRT, sigAbortHandler );
    179 
    180         // std::cout << "main" << std::endl;
     210        Signal( SIGSEGV, sigSegvBusHandler, SA_SIGINFO );
     211        Signal( SIGBUS, sigSegvBusHandler, SA_SIGINFO );
     212        Signal( SIGFPE, sigFpeHandler, SA_SIGINFO );
     213        Signal( SIGABRT, sigAbortHandler, SA_SIGINFO );
     214
     215        // cout << "main" << endl;
    181216        // for ( int i = 0; i < argc; i += 1 ) {
    182         //      std::cout << '\t' << argv[i] << std::endl;
     217        //      cout << '\t' << argv[i] << endl;
    183218        // } // for
    184219
     
    187222
    188223        if ( waiting_for_gdb ) {
    189                 std::cerr << "Waiting for gdb" << std::endl;
    190                 std::cerr << "run :" << std::endl;
    191                 std::cerr << "  gdb attach " << getpid() << std::endl;
     224                cerr << "Waiting for gdb" << endl;
     225                cerr << "run :" << endl;
     226                cerr << "  gdb attach " << getpid() << endl;
    192227                raise(SIGSTOP);
    193228        } // if
     
    259294                Stats::Time::StopBlock();
    260295
    261                 //std::cerr << "Post-Parse Check" << std::endl;
    262                 clearInnerLvalue( translationUnit );
    263                 assertTopLvalue( translationUnit );
    264 
    265296                // add the assignment statement after the initialization of a type parameter
    266297                PASS( "Validate", SymTab::validate( translationUnit, symtabp ) );
     
    281312                } // if
    282313
    283                 assertTopLvalue( translationUnit );
    284314                PASS( "Fix Labels", ControlStruct::fixLabels( translationUnit ) );
    285                 assertTopLvalue( translationUnit );
    286315                PASS( "Fix Names", CodeGen::fixNames( translationUnit ) );
    287                 assertTopLvalue( translationUnit );
    288316                PASS( "Gen Init", InitTweak::genInit( translationUnit ) );
    289                 assertTopLvalue( translationUnit );
    290317                PASS( "Expand Member Tuples" , Tuples::expandMemberTuples( translationUnit ) );
    291                 assertTopLvalue( translationUnit );
    292318                if ( libcfap ) {
    293319                        // generate the bodies of cfa library functions
     
    313339                } // if
    314340
    315                 assertTopLvalue( translationUnit );
    316 
    317341                PASS( "Resolve", ResolvExpr::resolve( translationUnit ) );
    318342                if ( exprp ) {
     
    321345                } // if
    322346
    323                 clearInnerLvalue( translationUnit );
    324                 assertTopLvalue( translationUnit );
    325 
    326347                // fix ObjectDecl - replaces ConstructorInit nodes
    327348                PASS( "Fix Init", InitTweak::fix( translationUnit, buildingLibrary() ) );
    328                 clearInnerLvalue( translationUnit );
    329                 assertTopLvalue( translationUnit );
    330349                if ( ctorinitp ) {
    331350                        dump ( translationUnit );
     
    334353
    335354                PASS( "Expand Unique Expr", Tuples::expandUniqueExpr( translationUnit ) ); // xxx - is this the right place for this? want to expand ASAP so tha, sequent 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
    336                 assertTopLvalue( translationUnit );
    337355
    338356                PASS( "Translate EHM" , ControlStruct::translateEHM( translationUnit ) );
    339                 assertTopLvalue( translationUnit );
    340357
    341358                PASS( "Gen Waitfor" , Concurrency::generateWaitFor( translationUnit ) );
    342                 clearInnerLvalue( translationUnit );
    343                 assertTopLvalue( translationUnit );
    344359
    345360                PASS( "Convert Specializations",  GenPoly::convertSpecializations( translationUnit ) ); // needs to happen before tuple types are expanded
    346                 clearInnerLvalue( translationUnit );
    347                 assertTopLvalue( translationUnit );
    348361
    349362                PASS( "Expand Tuples", Tuples::expandTuples( translationUnit ) ); // xxx - is this the right place for this?
    350                 assertTopLvalue( translationUnit );
    351363
    352364                if ( tuplep ) {
     
    356368
    357369                PASS( "Virtual Expand Casts", Virtual::expandCasts( translationUnit ) ); // Must come after translateEHM
    358                 assertTopLvalue( translationUnit );
    359370
    360371                PASS( "Instantiate Generics", GenPoly::instantiateGeneric( translationUnit ) );
     
    363374                        return EXIT_SUCCESS;
    364375                } // if
    365                 clearInnerLvalue( translationUnit );
    366                 assertTopLvalue( translationUnit );
     376
    367377                PASS( "Convert L-Value", GenPoly::convertLvalue( translationUnit ) );
    368                 clearInnerLvalue( translationUnit );
    369                 assertTopLvalue( translationUnit );
    370378
    371379                if ( bboxp ) {
     
    374382                } // if
    375383                PASS( "Box", GenPoly::box( translationUnit ) );
    376                 clearInnerLvalue( translationUnit );
    377                 assertTopLvalue( translationUnit );
    378384
    379385                if ( bcodegenp ) {
     
    387393
    388394                CodeTools::fillLocations( translationUnit );
    389                 assertTopLvalue( translationUnit );
    390395                PASS( "Code Gen", CodeGen::generate( translationUnit, *output, ! genproto, prettycodegenp, true, linemarks ) );
    391396
     
    419424                return EXIT_FAILURE;
    420425        } catch ( ... ) {
    421                 std::exception_ptr eptr = std::current_exception();
     426                exception_ptr eptr = current_exception();
    422427                try {
    423428                        if (eptr) {
    424                                 std::rethrow_exception(eptr);
     429                                rethrow_exception(eptr);
    425430                        } else {
    426                                 std::cerr << "Exception Uncaught and Unknown" << std::endl;
    427                         } // if
    428                 } catch(const std::exception& e) {
    429                         std::cerr << "Uncaught Exception \"" << e.what() << "\"\n";
     431                                cerr << "Exception Uncaught and Unknown" << endl;
     432                        } // if
     433                } catch(const exception& e) {
     434                        cerr << "Uncaught Exception \"" << e.what() << "\"\n";
    430435                } // try
    431436                return EXIT_FAILURE;
     
    438443
    439444
    440 static const char optstring[] = ":hlLmNnpP:S:twW:D:";
     445static const char optstring[] = ":c:ghlLmNnpP:S:twW:D:";
    441446
    442447enum { PreludeDir = 128 };
    443448static struct option long_opts[] = {
     449        { "colors", required_argument, nullptr, 'c' },
     450        { "gdb", no_argument, nullptr, 'g' },
    444451        { "help", no_argument, nullptr, 'h' },
    445452        { "libcfa", no_argument, nullptr, 'l' },
     
    453460        { "statistics", required_argument, nullptr, 'S' },
    454461        { "tree", no_argument, nullptr, 't' },
    455         { "gdb", no_argument, nullptr, 'g' },
    456462        { "", no_argument, nullptr, 0 },                                        // -w
    457463        { "", no_argument, nullptr, 0 },                                        // -W
     
    461467
    462468static const char * description[] = {
    463         "print help message",                                                           // -h
    464         "generate libcfa.c",                                                            // -l
    465         "generate line marks",                                                          // -L
    466         "do not replace main",                                                          // -m
    467         "do not generate line marks",                                           // -N
    468         "do not read prelude",                                                          // -n
     469        "diagnostic color: never, always, or auto.",          // -c
     470        "wait for gdb to attach",                             // -g
     471        "print help message",                                 // -h
     472        "generate libcfa.c",                                  // -l
     473        "generate line marks",                                // -L
     474        "do not replace main",                                // -m
     475        "do not generate line marks",                         // -N
     476        "do not read prelude",                                // -n
    469477        "generate prototypes for prelude functions",            // -p
    470         "print",                                                                                        // -P
     478        "print",                                              // -P
    471479        "<directory> prelude directory for debug/nodebug",      // no flag
    472480        "<option-list> enable profiling information:\n          counters,heap,time,all,none", // -S
    473         "building cfa standard lib",                                                                    // -t
    474         "wait for gdb to attach",                                                                       // -g
    475         "",                                                                                                     // -w
    476         "",                                                                                                     // -W
    477         "",                                                                                                     // -D
     481        "building cfa standard lib",                          // -t
     482        "",                                                   // -w
     483        "",                                                   // -W
     484        "",                                                   // -D
    478485}; // description
    479486
     
    543550        while ( (c = getopt_long( argc, argv, optstring, long_opts, nullptr )) != -1 ) {
    544551                switch ( c ) {
     552                  case 'c':                                                                             // diagnostic colors
     553                        if ( strcmp( optarg, "always" ) == 0 ) {
     554                                ErrorHelpers::colors = ErrorHelpers::Colors::Always;
     555                        } else if ( strcmp( optarg, "never" ) == 0 ) {
     556                                ErrorHelpers::colors = ErrorHelpers::Colors::Never;
     557                        } else if ( strcmp( optarg, "auto" ) == 0 ) {
     558                                ErrorHelpers::colors = ErrorHelpers::Colors::Auto;
     559                        } // if
     560                        break;
    545561                  case 'h':                                                                             // help message
    546562                        usage( argv );                                                          // no return
Note: See TracChangeset for help on using the changeset viewer.